你也可以查看Dapper-Extensions个.
Dapper扩展是一个小型库,它通过添加
您的POCOS的基本CRUD操作(GET、INSERT、UPDATE、DELETE).
它有一个auto class mapper,您可以在其中指定自定义字段映射.例如:
public class CodeCustomMapper : ClassMapper<Code>
{
public CodeCustomMapper()
{
base.Table("Codes");
Map(f => f.Id).Key(KeyType.Identity);
Map(f => f.Type).Column("Type");
Map(f => f.Value).Column("Code");
Map(f => f.Description).Column("Foo");
}
}
然后你只要做:
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
var code= new Code{ Type = "Foo", Value = "Bar" };
int id = cn.Insert(code);
cn.Close();
}
请记住,您必须将自定义 map 与POCO类保存在同一程序集中.该库使用反射来查找自定义映射,并且它只扫描一个程序集.
Update:
现在可以使用SetMappingAssembly注册要扫描的程序集列表:
DapperExtensions.SetMappingAssemblies(new[] { typeof(MyCustomClassMapper).Assembly });