asp.net - 如何在 Entity Framework Core 2.1 中播种枚举

标签 asp.net entity-framework asp.net-core .net-core entity-framework-core

我是 EF Core 的新手,正在尝试播种枚举。

根据Data Seeding ,此功能是 EF Core 2.1 的新增功能。

我回顾了几个解决方案,包括这个 SO solution by Blake Mumford ,但这对我不起作用,因为枚举不是引用类型。

我的目标(在迁移的帮助下)是将 Category 枚举填充到一个名为 Category 的新 SQL 表中,并让我的 Payment 表包含一个引用 Category 表作为外键的列。

任何帮助将不胜感激。谢谢。

public partial class Payment
{
    public int PaymentId { get; set; }
    public DateTime PostedDate { get; set; }
    public string Vendor { get; set; }
    public decimal Amount { get; set; }

    public virtual Category Category { get; set; }
}

public enum Category
{
    Restaurants,
    Groceries,
    [Display(Name = "Home Goods")]
    HomeGoods,
    Entertainment
}

public partial class MyDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Error: The type Category must be a reference type in order to use it as parameter TEntity in the 
        // genric type or method ModelBuilder.Entity<TEntity>()

        modelBuilder.Entity<Category>().HasData(Category.Restaurants, 
                                                Category.Groceries, 
                                                Category.HomeGoods, 
                                                Category.Entertainment);
    }
}

最佳答案

这就是我所做的,希望它适用于您的环境。

环境

项目框架

  • .NetCore 3.0

  • 核桃:
  • EFCore 3.1.2
  • EFCore.Design 3.1.2
  • EFCore.Tools 3.1.2
  • EFCore.Relational 3.1.2
  • EFCore.SqlServer 3.1.2

  • 实现
  • 添加 OnModelCreating
    public partial class MyDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Error: The type Category must be a reference type in order to use it as parameter TEntity in the 
            // genric type or method ModelBuilder.Entity<TEntity>()
    
            modelBuilder.Entity<UserRole>().HasData(EnumFunctions.GetModelsFromEnum<UserRole, UserRoleEnum>());
        }
    }
    
  • 创建一个通用枚举函数
    public static class EnumFunctions
    {
        public static IEnumerable<TModel> GetModelsFromEnum<TModel, TEnum>() where TModel : IEnumModel<TModel, TEnum>, new()
        {
            var enums = new List<TModel>();
            foreach (var enumVar in (TEnum[])Enum.GetValues(typeof(TEnum)))
            {
                enums.Add(new TModel
                {
                    Id = enumVar,
                    Name = enumVar.ToString()
                });
            }
    
            return enums;
        }
    }
    
  • 创建接口(interface)
    public interface IEnumModel<TModel, TModelIdType>
    {
        TModelIdType Id { get; set; }
        string Name { get; set; }
    }
    
  • 将接口(interface)应用于模型
    [Table("UserRoles")]
    public class UserRole : IEnumModel<UserRole, UserRoleEnum>
    {
        [Key]
        public UserRoleEnum Id { get; set; }
        public string Name { get; set; }       
    }
    
  • 添加您的迁移
    PM> add-migration SeedUserRoleTable
    

  • 您应该看到添加的迁移
  • 使用种子信息更新数据库
    PM> update-database
    
  • 关于asp.net - 如何在 Entity Framework Core 2.1 中播种枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53620246/

    相关文章:

    asp.net - WebForm_PostBackOptions 文档

    c# - 更新数据库 : "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

    c# - 无法使用 DI 选项在 AspNetCore 2.0 应用程序中创建 DbContext 的实例

    javascript - 构建网站时,我应该在特定用户控件还是母版页中引用 javascript 库?

    css - 选项卡内的 Bootstrap slider

    c# - 如何在 SqlDataSource select Command where 条件中传递属性值?

    entity-framework - EF4 - 强制执行顺序

    entity-framework - 带有字段组合列表的Where子句

    c# - 分页和页面冲突 - ASP.Net Core Razor 页面与 MVC Controller 方法分页冲突

    c# - 在 Visual Studio Code 中恢复依赖项使用了错误的源/提要