Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AnsiStrings be used by default with Dapper?

Tags:

c#

dapper

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.

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?

Update

Just changing the typeMap was not sufficient I needed to alter some if (dbType == DbType.String) checks too. Now it works!

like image 884
John Foster Avatar asked Jun 17 '11 12:06

John Foster


1 Answers

You can accomplish this without modifying the source code.

Dapper.SqlMapper.AddTypeMap(typeof(string), System.Data.DbType.AnsiString);

Setting this once will adjust all of your strings to varchar.

like image 111
BillRob Avatar answered Sep 20 '22 21:09

BillRob