Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a table corresponding to enum in EF Core Code First?

Tags:

How would one turn the enums used in an EF Core database context into lookup tables and add the relevant foreign keys?


  • Same as EF5 Code First Enums and Lookup Tables but for EF Core instead of EF 6
  • Related to How can I make EF Core database first use Enums?
like image 994
Tim Abell Avatar asked May 16 '18 15:05

Tim Abell


People also ask

How do I turn an enum into a list?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Can we assign string to enum?

We can use Enum. GetName static method to convert an enum value to a string. The following code uses Enum. GetName static method that takes two arguments, the enum type and the enum value.


1 Answers

You can use an enum in your code and have a lookup table in your db by using a combination of these two EF Core features:

  • Value Conversions - to convert the enum to int when reading/writing to db
  • Data Seeding - to add the enum values in the db, in a migration

Here below a data model example:

public class Wine {     public int WineId { get; set; }     public string Name { get; set; }      public WineVariantId WineVariantId { get; set; }     public WineVariant WineVariant { get; set; } }  public enum WineVariantId : int {     Red = 0,     White = 1,     Rose = 2 }  public class WineVariant {     public WineVariantId WineVariantId { get; set; }     public string Name { get; set; }      public List<Wine> Wines { get; set; } } 

Here the DbContext where you configure value conversions and data seeding:

public class WineContext : DbContext {     public DbSet<Wine> Wines { get; set; }     public DbSet<WineVariant> WineVariants { get; set; }      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)     {         optionsBuilder.UseSqlite("Data Source=wines.db");     }      protected override void OnModelCreating(ModelBuilder modelBuilder)     {         modelBuilder             .Entity<Wine>()             .Property(e => e.WineVariantId)             .HasConversion<int>();          modelBuilder             .Entity<WineVariant>()             .Property(e => e.WineVariantId)             .HasConversion<int>();          modelBuilder             .Entity<WineVariant>().HasData(                 Enum.GetValues(typeof(WineVariantId))                     .Cast<WineVariantId>()                     .Select(e => new WineVariant()                     {                         WineVariantId = e,                         Name = e.ToString()                     })             );     } } 

Then you can use the enum values in your code as follow:

db.Wines.Add(new Wine {     Name = "Gutturnio",     WineVariantId = WineVariantId.Red, });  db.Wines.Add(new Wine {     Name = "Ortrugo",     WineVariantId = WineVariantId.White, }); 

Here is what your db will contain:

WineVariants table

Wines table

I published the complete example as a gist: https://gist.github.com/paolofulgoni/825bef5cd6cd92c4f9bbf33f603af4ff

like image 119
Paolo Fulgoni Avatar answered Oct 08 '22 00:10

Paolo Fulgoni