【问题标题】:EF Core 2.2.4 and seed dataEF Core 2.2.4 和种子数据
【发布时间】:2020-12-13 12:37:54
【问题描述】:

我在 DataContext 上有以下代码:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration<CollectionSite>(new CollectionSiteMap());
        modelBuilder.ApplyConfiguration<TestOrderAlcoholResult>(new TestOrderAlcoholResultMap());
        //.... other configurations

        modelBuilder.Entity<ButtonStyle>().HasData(new ButtonStyle()
        {
            Label = "Sharp edges",
            Style = "border-radius: 0",
            IsDefault = true,
            Id = 1
        });
        modelBuilder.Entity<ColorScheme>().HasData(new ColorScheme()
        {
            Primary = "#a21521",
            Secondary = "#fcf7f8",
            Text = "#ced3dc",
            Background = "#4e8097",
            Shade = "#90c2e6",
            IsDefault = true,
            Id = 1
        });
        //.... seed other data

        base.OnModelCreating(modelBuilder);
    }

update-database 之后的数据不会添加到表中。怎么了?

【问题讨论】:

    标签: entity-framework entity-framework-core ef-core-2.2


    【解决方案1】:

    当您更改 Models 或在 DbContext 中进行更改时调用 OnModelCreating,然后使用 Add-Migrationupdate-database

    OnModelCreating 并非每次使用update-database 时都运行。

    根据Microsoft documentation,这是使用种子数据

    的更好方法
    public static void Main(string[] args)
    {
         var host = CreateWebHostBuilder(args).Build();
    
        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            try
            {
                var context = services.GetRequiredService<DbContext>();
                DbInitializer.InitializeData(context);
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An error occurred while seeding the database.");
            }
        }
    
        host.Run();
    }
    
    public static class DbInitializer
    {
        public static void InitializeData(DbContext context)
        {
            context.Database.EnsureCreated();
            context.ButtonStyle.Add(new ButtonStyle()
            {
                Label = "Sharp edges",
                Style = "border-radius: 0",
                IsDefault = true,
                Id = 1
            });
            context.ColorScheme.Add(new ColorScheme()
            {
                Primary = "#a21521",
                Secondary = "#fcf7f8",
                Text = "#ced3dc",
                Background = "#4e8097",
                Shade = "#90c2e6",
                IsDefault = true,
                Id = 1
            });
            context.SaveChanges();
        }
    }
    

    在这种情况下InitializeData每次运行程序都会检查,如果数据库中没有记录,则再次在数据库中保存一条新记录。

    【讨论】:

    • 我得到“SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'DeviceModel' 中插入标识列的显式值。”如果记录已经存在
    • @OlegSh 显示您的DeviceModel 模型
    • @OlegSh 在SQL 中将IDENTITY_INSERT 设置为开启。在SQL SET IDENTITY_INSERT DeviceModel ON 中运行此查询。这个link 可能对你有帮助
    猜你喜欢
    • 2020-08-30
    • 2019-10-07
    • 2021-04-26
    • 2017-06-23
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多