简体   繁体   English

用小巧的插件进行枚举处理

[英]enum handling on insert with dapper

How can i make dapper use the string value of an enum. 我如何使dapper使用枚举的字符串值。 In the example below it uses the numerical value of the enum. 在下面的示例中,它使用枚举的数值。 On read from the database, dapper correctly converts string to enum. 从数据库读取后,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);

In this case in the database in the column Cat I have value 1 instead of A 在这种情况下,数据库中Cat列的值是1而不是A

我认为最简单的方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM