Dapper.NET: varchar (4000) parameter by default

I use Dapper.NET and when I run the following code:

using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); con.Execute(@" insert Clients(name) values(@Name)", new {Name = "John"}); con.Close(); } 

The query that he executes is as follows:

 (@Name nvarchar(4000)) insert Clients(name) values(@Name) 

And my question is: why does Dapper translate the string to nvarchar (4000)? I mean ... in the database, the name field is nvarchar (50) ...

Does anyone encounter this error? How do you fix this? Did you find another mistake?

+5
source share
1 answer

It's not a mistake. Dapper should choose the SQL data type for the string parameter without looking at the database structure (not to mention analyzing your query and determining that you are inserting the parameter into a specific column).

Imagine if you do this:

 insert Clients(name) values(@Name + 'abc') 

Should Dapper find out that @Name can contain up to 47 characters?

You can be more specific about the size of your parameter if you want:

 con.Execute(@" insert Clients(name) values(@Name)", new { Name = new DbString { Value = "John", Length = 50 }}); 
+7
source

All Articles