4

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

1
  • Not sure if you can. You cannot with EF, think that Enum are internally represented as numeric values, the name is just something you use from C# code. Commented Mar 6, 2015 at 16:30

1 Answer 1

2

The easiest way I think is:

connection.Execute("Insert into Products (Cat, Id) Values ", new { p.Id, Cat = p.Cat.ToString());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.