I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int.
I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So, instead of only getting a table corresponding to Department in the example on MSDN, I'd also like to see a second table populated by the items from Faculty.
public enum Faculty { Eng, Math, Eco }
public partial class Department
{
[Key] public Guid ID { get; set; }
[Required] public Faculty Name { get; set; }
}
Researching the issue, I stumbled upon a solution, which suggests creating a table for the enumeration and populating it explicitly by seeding.
It appear to me as a cumbersome approach and a lot of work that should be handled automagically. After all, the system knows what actual values that constitute the enumeration. From DB point of view it's still data rows, just as the entities that I create but from OO aspect, it's not really a data - rather a type (loosely expressed) that can assume a finite and onbeforehand known number of states.
Is the approach of populating the table "manually" recommended?
Another approach that works (and feels simpler to me) in EF Core:
Your Enum
Db Tables
Your DbContext
In code I basically only use the enum Color (never ColorDto). But it's still nice to have the 'Colors' table with an FK in the 'CustomObjects' table for sql queries and views.
Based on @Alberto Monteiro answer i've created generic class in case when you have several tables. The notice here is that Id is the type of TEnum. Using it in such way will provide option to use Enum for declaring property type.
By default Enum using integers, so the db provider will create field with "int" type.
EnumTable.cs
ExceptionHelpers.cs
Now you just can inherit the EnumTable
Seed the values
Another possibility, if you want to keep your model simpler, POCO style, use the enum as a property that will be stored as an integer by entity framework.
Then, if you want the "enum tables" to be created and updated in your DB, I recommend using the nuget package https://github.com/timabell/ef-enum-to-lookup and use it in a EF Migration seed method for example:
This will create the "Shape" table with 2 rows named Square and Round, with the relevant foreign key constraint in the table "Foo"
Alberto Monteiro answered this very well. I had to make a few adjustments to get it to work with EF core.
Rename your enum and add description decorators
Create a class that represent the table
Your model reference the class
Create a extension method to get description from enum and seed values
Add the seed in YourDbContext.cs
Add the enum table in your DbContext
Use it
To remember
If you don't add virtual in Faculty property, you must use Include method from DbSet to do Eager Load
If Faculty property is virtual, then just use it
Excellent @AlbertoMonterio! To get this to work with ASP.NET CORE / EF Core I made a few adjustments to Alberto's solution.
For brevity, only the modifications are shown below:
Create a extension method to get description from enum and seed values
Add the seed in Configuration.csAdd Seeding to
OnModelCreating
of DataContextSince EF doesn't handle it automatically, yes, this is the recommend way.
I suggest some modifications in article that you provided.
Rename your enum
Create a class that represent the table
Your model reference the class
Create a extension method to get description from enum and seed values
Add the seed in Configuration.cs
Add the enum table in your DbContext
Use it
To remember
If you don't add virtual in Faculty property, you must use Include method from DbSet to do Eager Load
If Faculty property is virtual, then just use it