SQL and Dapper Performance Implicit Conversion

[亡魂溺海] 提交于 2020-06-26 04:49:17

问题


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

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