问题描述
如果数据库不存在,我将尝试自动生成该数据库,并运行 Seed()
方法来填充数据。在我的数据库上下文构造函数中,我有这个:
I'm trying to automatically generate my database if it doesn't exists and run the Seed()
method to populate the data. In my Database Context constructor I have this:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
这很好用,我的数据库是根据需要自动创建所有表的,但是好像 Seed()
方法未被调用,我的数据库为空。这是我的课程:
This works great, my database is automatically created with all the tables as I want, but it seems like the Seed()
method is not being called, my database is empty. This is my class:
internal sealed class Configuration : DbMigrationsConfiguration<Context.MyDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "[email protected]", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
base.Seed(context);
}
}
当我运行 Update-
在创建数据库后填充数据,但是使用 Nuget
控制台中的数据库 MigrateDatabaseToLatestVersion
Seed()
方法未调用。
When I run Update-Database
in the Nuget
console the data is populated after database creation, but with MigrateDatabaseToLatestVersion
the Seed()
method is not called.
会发生什么?我尝试手动运行从:
What can be happening? I tried manually running migrations as taken from here:
var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);
var migrator = new DbMigrator(configuration);
migrator.Update();
但也不起作用。
编辑:
确定,经过更多测试后,我发现 Seed()
方法运行,但仅在数据库已经存在时才运行,即第一次运行数据库时第一次创建 Seed()
方法没有执行,但是当我第二次运行我的应用程序时, Seed()
get的执行。我还必须添加 context.SaveChanges()
才能使其工作(感谢@DavidG在评论中):
Ok, after some more testing I found that the Seed()
method runs but only when the database already exists, that is, on the first run when the database is created for the first time the Seed()
method is not executed, but when I run my app the second time Seed()
get's executed. I also had to add context.SaveChanges()
in order for it to work (thanks to @DavidG in the comments):
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "[email protected]", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
context.SaveChanges();
base.Seed(context);
}
所以也许我可以手动调用 Seed()
在 Configuration()
中,并进行一些检查,以避免添加重复数据或修改已经存在的数据。
So maybe I can manually call Seed()
inside Configuration()
and do some checking to avoid adding duplicating data or modifying data that already exists.
推荐答案
我最终使用了 Configuration
类:
public class Configuration : DbMigrationsConfiguration<Context.MyDBContext>
{
private readonly bool _pendingMigrations;
public Configuration()
{
AutomaticMigrationsEnabled = true;
// Check if there are migrations pending to run, this can happen if database doesn't exists or if there was any
// change in the schema
var migrator = new DbMigrator(this);
_pendingMigrations = migrator.GetPendingMigrations().Any();
// If there are pending migrations run migrator.Update() to create/update the database then run the Seed() method to populate
// the data if necessary
if (_pendingMigrations)
{
migrator.Update();
Seed(new Context.MyDBContext());
}
}
protected override void Seed(Context.MyDBContext context)
{
// Microsoft comment says "This method will be called after migrating to the latest version."
// However my testing shows that it is called every time the software starts
// Run whatever you like here
// Apply changes to database
context.SaveChanges();
base.Seed(context);
}
}
因此,创建数据库时以及还有待完成的迁移时,将调用Seed()
方法。
这是我的 MyDBContext
类:
public class MyDBContext: DbContext
{
public MyDBContext() : base(AppSettings.DBConnectionString)
{
}
public static MyDBContext Create()
{
return new MyDBContext();
}
public DbSet<User> Users { get; set; }
public DbSet<Entries> Entries { get; set; }
}
这篇关于MigrateDatabaseToLatestVersion没有运行Seed()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!