Skip to content

Instantly share code, notes, and snippets.

@paolofulgoni
Created September 15, 2019 20:45
Show Gist options
  • Save paolofulgoni/825bef5cd6cd92c4f9bbf33f603af4ff to your computer and use it in GitHub Desktop.
Save paolofulgoni/825bef5cd6cd92c4f9bbf33f603af4ff to your computer and use it in GitHub Desktop.
Enums in EF Core
<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>
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");
}
}
}
}
@paolofulgoni
Copy link
Author

@rich0726
Copy link

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?

@paolofulgoni
Copy link
Author

@amunim
Copy link

amunim commented May 4, 2022

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)

@Sergio1C
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment