简体   繁体   中英

Dapper.NET: The varchar(4000) default

I am using Dapper.NET and when i execute the next code:

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

The query that it executes is the next one:

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

And my question is: why is Dapper translating a string to a nvarchar(4000)? I mean... on the database, the name field is a nvarchar(50) ...

Does anybody face this bug? How do you fix it? Have you found another bug like this?

This is not a bug. Dapper has to pick a SQL data-type for a string parameter, without looking at the database structure (not to mention parsing your query and determining that you're inserting the parameter into a particular column).

Imagine if you were doing this:

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

Should Dapper have to figure out that @Name can be up to 47 characters?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM