-
-
Save Sergio1C/1f08c203011d42c4fcf7a7e43ff590a5 to your computer and use it in GitHub Desktop.
Enums in EF Core
This file contains hidden or 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" /> | |
| <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6"> | |
| </ItemGroup> | |
| </Project> |
This file contains hidden or 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()) | |
| { | |
| //You have to do migration by yourself - execute "Add-Migration Init" on Package Manager Console. | |
| //Then - run this code. | |
| 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"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment