可以AnsiStrings默认与小巧玲珑的使用? [英] Can AnsiStrings be used by default with Dapper?
问题描述
我使用的是小巧玲珑的反对,其中的字符串主要存储在的VarChar
列的数据库。默认情况下小巧玲珑使用的NVarChar
生成查询时的参数,虽然我可以包装每一个和我一起 DbString
利用每一个字符串参数它倒是很大默认使用AnsiStrings并使用 DbString
为的NVarChar
情况。
I'm using Dapper against a database where strings are stored primarily in VarChar
columns. By default Dapper uses NVarChar
parameters when generating queries and while I can wrap each and every string parameter I use with DbString
it'd be great to use AnsiStrings by default and use DbString
for the NVarChar
case.
<击>我试图改变在小巧玲珑源的类型映射从 DbType.String
到 DbType.AnsiString
然而,这似乎导致IL一代的参数代表一个错误(抛出一个 InvalidProgramException
)。击>
I tried changing the type map in the Dapper source from DbType.String
to DbType.AnsiString
however that seems to cause an error in the IL generation for the parameters delegate (throws an InvalidProgramException
).
有没有更简单的方式来做到这一点?
Is there an easier way to do this?
更新
只是改变了类型映射不足以我需要改变某些如果(DBTYPE == DbType.String)
检查过。现在,它的作品!
Just changing the typeMap was not sufficient I needed to alter some if (dbType == DbType.String)
checks too. Now it works!
推荐答案
可以做到这一点无需修改源代码。
You can accomplish this without modifying the source code.
Dapper.SqlMapper.AddTypeMap(typeof(string), System.Data.DbType.AnsiString);
设置这个一度将您的所有字符串的调整VARCHAR。
Setting this once will adjust all of your strings to varchar.
这篇关于可以AnsiStrings默认与小巧玲珑的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!