Mapping char(8) to string property with Dapper

▼魔方 西西 提交于 2019-12-06 23:36:40

问题


I have the following table, abridged:

CREATE TABLE [dbo].[TERMINAL] (
    [TERM_CODEID]    SMALLINT     NOT NULL,
    [TERM_ACTIVE]    SMALLINT     NOT NULL,
    [TERM_NAME]      VARCHAR (30) NOT NULL,
    [TERM_SLA]       CHAR (8)     NOT NULL,
    [TERM_SERIAL]    VARCHAR (8)  NULL,
    [TERM_VERSION]   VARCHAR (8)  NULL,

    [TERM_STATUS]    INT          NULL,
)

When I try the following Dapper code - and I'm a complete Dapper novice, found it yesterday - I get an error:

using (var conn = new SqlConnection("data source=ourServer; initial catalog=ourDb;user id=sa;password=ourPassword;"))
{
    conn.Open();
    var terms = conn.Query<Terminal>("select * from TERMINAL");
}

The error is:

Error parsing column 3 (TERM_SLA=01010B01 - String)

I can see no reason why anything should even have to 'parse' a string, never mind experience an error while doing so. What could be causing this>


回答1:


Dapper expects the .NET data type to be exactly the same as in your database. Term_Sla needs to be of type String.




回答2:


Here is my experience. i hope this may help, someone:

I had the same error, and .net type was matching the Sql data type; except that some data was null. So make sure that your sql data is not nullable, otherwise adapt your .net property type accordingly.



来源:https://stackoverflow.com/questions/17038101/mapping-char8-to-string-property-with-dapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!