Created
September 15, 2019 20:45
-
-
Save paolofulgoni/825bef5cd6cd92c4f9bbf33f603af4ff to your computer and use it in GitHub Desktop.
Enums in EF Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.2</TargetFramework> | |
<RootNamespace>EFCoreEnums</RootNamespace> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.6" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.EntityFrameworkCore; | |
namespace EFCoreEnums | |
{ | |
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, | |
White, | |
Rose | |
} | |
public class WineVariant | |
{ | |
public WineVariantId WineVariantId { get; set; } | |
public string Name { get; set; } | |
public List<Wine> Wines { get; set; } | |
} | |
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() | |
}) | |
); | |
} | |
} | |
public static class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
using (var db = new WineContext()) | |
{ | |
await db.Database.MigrateAsync(); | |
db.Wines.Add(new Wine | |
{ | |
Name = "Gutturnio", | |
WineVariantId = WineVariantId.Red, | |
}); | |
db.Wines.Add(new Wine | |
{ | |
Name = "Ortrugo", | |
WineVariantId = WineVariantId.White, | |
}); | |
await db.SaveChangesAsync(); | |
} | |
using (var db = new WineContext()) | |
{ | |
var gutturnio = await db.Wines | |
.AsNoTracking() | |
.Include(w => w.WineVariant) | |
.FirstAsync(w => w.Name == "Gutturnio"); | |
Console.WriteLine($"{gutturnio.Name} is a {gutturnio.WineVariant.Name} wine"); | |
} | |
} | |
} | |
} |
How would this model change if you had a one to many relationship to the Enum? Like what if it was People and Sports (an enum of typical sports), where people could play multiple sports and you wanted to map that relationship through EF?
Hi @rich0726, that would be a many to many relationship, take a look at here: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#many-to-many
What changes would we have to do to this, if we need it as a many-to-many relationship, then convert it back to a ICollection of enum(or class with enum as Id)
It should be updated a bit as here: https://gist.github.com/Sergio1C/1f08c203011d42c4fcf7a7e43ff590a5/revisions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my answer to this StackOverflow question: How to create a table corresponding to enum in EF Core Code First?