使用小巧玲珑的插入物上的枚举处理

时间:2015-03-06 16:26:44

标签: c# dapper

如何让dapper使用枚举的字符串值。在下面的示例中,它使用枚举的数值。 从数据库读取时,dapper正确地将字符串转换为枚举。

public enum Category { A, B }

public Product 
{ 
    public Category Cat {get;set;}
    public int Id {get;set;}
}

Product p  = new Product() {Cat = Category.A, Id=22} ;
connection.Execute("Insert into Products (Cat, Id) Values ",p);

在这种情况下,在Cat列中的数据库中,我的值为1,而不是A

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是:

connection.Execute("Insert into Products (Cat, Id) Values ", new { p.Id, Cat = p.Cat.ToString());