Dapper对它映射的类型很挑剔。这样可以保护您免受以后弹出的各种讨厌的错误的侵害。
例如,您的数据库可能返回:
010hello
10a10
374837483748374837483748374834784378437438743874384738473
将此类资料映射到 Int32
就是说,精简程序可以遵循两种策略,不需要更改IL生成。
选项1:阴影属性
class Foo
{
public int Age
{
get
{
int age;
int.TryParse(ageString, out age);
return age;
}
}
string ageString;
}
\\ usage
cnn.Query<Foo>("select ageString from TableWithString");
Run Code Online (Sandbox Code Playgroud)
选项2,在SQL中强制转换
cnn.Query<Bar>("select cast(ageString as int) Age from TableWithString");
Run Code Online (Sandbox Code Playgroud)
在Dapper中没有一种干净的方法来扩展映射功能,如果这样做,您将不得不将我们随时间增加的所有修订合并到本地副本中。