How to map Enum property in Entity Framework 6 Code First and Fluent Api

c# code-first ef-fluent-api entity-framework enums

Question

I've been searching the internet how to map enum in ASP.NET MVC project using Entity Framework 6 (Code First and Fluent Api), the results of my search showed that this type of mapping is not trival as in we do in Java (Hibernate + JPA) .

I found an article that gives an idea of how to solve the problem, but do not know if there is another way.

Article: http://eliot-jones.com/2015/03/entity-framework-enum

They could share experience on this type of mapping?

What would be the correct or official way to map Enum?

I'm working with the possibility that the field is string in the database and for this reason I would like to know if you can work with the ordinal type or the name of the enumeration.

I wait for a return and I thank you for your attention.

1
1
6/28/2016 6:47:55 PM

Popular Answer

In Hibernate you should map all entities yourself but Entity Framework has a default behavior for mapping entities that you register with dbset in dbcontext.

public DbSet<Entity> Entities { get; set; }

you should use fluent api where default behavior of EF didn't satisfies your purpose. But in your matter (Enums in EF) default behavior will work properly and you dont need to do any thing. It saves enume as int in database. Entity Framework Enum Support

0
11/14/2016 10:36:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow