【问题标题】:Database SetInitializer is not found in MySql.Data.EntityFrameworkCore在 MySql.Data.EntityFrameworkCore 中找不到数据库 SetInitializer
【发布时间】:2020-02-01 01:26:18
【问题描述】:

我正在使用MySql.Data.EntityFrameworkCore 提供程序连接mysql。 现在我想在使用DropCreateDatabaseAlways初始化数据库时播种数据,但是当我尝试像这样使用它时找不到它

Database.SetInitializer<SmContext>(new SMDbInitializer<SmContext>());

我的SMDbInitializer 会是这个样子。

private class SMDbInitializer<T> : DropCreateDatabaseAlways<SmContext> 
{

      protected override void Seed(SmContext context) 
      {

         //Some code
         base.Seed(context);
      }
} 

【问题讨论】:

  • EF Core != EF 6. 实体框架核心是实体框架更高级版本的完全重写。 DropCreateDatabaseAlways 是 EF 的一个类,而不是 EF Core。有关 EF Core 种子模式,请参阅this answer

标签: c# mysql asp.net asp.net-core entity-framework-core


【解决方案1】:

您可以创建一个DbInitializer.cs 并调用dbContext.Database.EnsureDeleted();删除现有数据库。

public static class DbInitializer
{
    public static void Initialize(MyDbContext context)
    {
        //Ensures that the database for the context does not exist. If it does not exist, no action is taken. 
        //If it does exist then the database is deleted.
        //Warning: The entire database is deleted an no effort is made to remove just the database objects that are used by the model for this context.
        context.Database.EnsureDeleted();

        //create the database
        context.Database.EnsureCreated();

        // Look for any students.
        if (context.Students.Any())
        {
            return;   // DB has been seeded
        }

        var students = new Student[]
        {
        new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
        new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
        new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
        new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},

        };
        foreach (Student s in students)
        {
            context.Students.Add(s);
        }
        context.SaveChanges();

        //Applies any pending migrations for the context to the database.Will create the database if it does not already exist.
        //Note that this API is mutually exclusive with DbContext.Database.EnsureCreated().
        //EnsureCreated does not use migrations to create the database and therefore the
        //database that is created cannot be later updated using migrations.
        context.Database.Migrate();
    }
}

在 Program.cs 中,修改 Main 方法以在应用程序启动时执行以下操作:

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<SchoolContext>();
        DbInitializer.Initialize(context);
    }
    catch (Exception ex)
    {
        var logger = services.GetRequiredService<ILogger<Program>>();
        logger.LogError(ex, "An error occurred while seeding the database.");
    }
}

 host.Run();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2016-07-30
    相关资源
    最近更新 更多