简体   繁体   English

EF Core 2.2.4 和种子数据

[英]EF Core 2.2.4 and seed data

I have the following code on DataContext:我在 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);
    }

but data is not added to tables after update-database .但是在update-database之后数据不会添加到表中。 What is wrong?怎么了?

The OnModelCreating called when you changed your Models or make changes in your DbContext then use Add-Migration and update-database . OnModelCreating在您更改模型或在DbContext 中进行更改时调用,然后使用Add-Migrationupdate-database

OnModelCreating does not run every time when you use update-database . OnModelCreating不会在您每次使用update-database时运行。

According to Microsoft documentation this is better way to use Seed Data根据Microsoft 文档,这是使用种子数据的更好方法

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();
    }
}

In this case InitializeData is checked every time your program is run, and if there is no record in the database, a new record is saved in the database again.在这种情况下,每次程序运行时都会检查InitializeData ,如果数据库中没有记录,则再次在数据库中保存一条新记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM