enum handling on insert with dapper

c# dapper

Question

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

Popular Answer

The easiest way I think is:

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


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why