简体   繁体   中英

enum handling on insert with dapper

How can i make dapper use the string value of an enum. In the example below it uses the numerical value of the enum. On read from the database, dapper correctly converts string to enum.

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

我认为最简单的方法是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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