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 enu
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:
using System;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using Microsoft.EntityFrameworkCore; //added
using Microsoft.EntityFrameworkCore.Metadata.Builders; //added
public static class Extensions
{
//unchanged from alberto answer
public static string GetEnumDescription(this TEnum item)
=> item.GetType()
.GetField(item.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.Cast()
.FirstOrDefault()?.Description ?? string.Empty;
//changed
public static void SeedEnumValues(this ModelBuilder mb, Func converter)
where T : class => Enum.GetValues(typeof(TEnum))
.Cast
OnModelCreating
of DataContextprotected override void OnModelCreating(ModelBuilder builder)
{
builder.SeedEnumValues(e => e);
}