Mapping char (8) to a string property using Dapper

I have the following table shortened:

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 newbie, found it yesterday - I get an error message:

 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"); } 

Error:

Column 3 parsing analysis (TERM_SLA = 01010B01 - String)

I see no reason why something should even โ€œparseโ€ the string, but you donโ€™t need to experience an error. What could be the reason for this>

+9
source share
2 answers

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

+11
source

Here is my experience. I hope this can help someone:

I had the same error and the .net type matched the Sql data type; except that some data was null. Therefore, make sure that your sql data is not nullified, otherwise change the type of your .net property accordingly.

+1
source

Source: https://habr.com/ru/post/947008/


All Articles