问题
How do we prevent SQL Implicit Conversions in Dapper?
We realized, we were conducting an SQL Implicit conversion, causing an Index Scan and Deadlocks. Dapper parameters are nvarchar, while SQL table columns are varchar. This caused all our sql columns convert into nvarchar.
We fixed the issue by going through all our embedded Dapper code and converting columns as cast(@SSN as varchar(9)), cast(@LastName as varcarh(25)), cast(@EmployeeId as varchar(10)
There has got to be an easier way, is there?
I read this blog, except we aren't setting strings like this example:
new { queryPlanHash = args[0], startDate = DateTime.Today.AddDays(-7) });
https://www.codeproject.com/articles/594133/bepluscarefulpluswithplusvarcharsplusinplusdapper
Is there something we can change in the connection string?
FinanceConnectionString "providerName="System.Data.SqlClient" connectionString="Data Source=(local);Initial Catalog=FinanceData;integrated security=SSPI;persist security info=False; Trusted_Connection=Yes" />
回答1:
You can configure Dapper to submit your strings always as varchar rather than nvarchar
Dapper.SqlMapper.AddTypeMap(typeof(string), System.Data.DbType.AnsiString);
Please see also Can AnsiStrings be used by default with Dapper?
来源:https://stackoverflow.com/questions/46659082/sql-and-dapper-performance-implicit-conversion