MigrateDatabaseToLatestVersion 否 运行 Seed() 方法
MigrateDatabaseToLatestVersion no running Seed() method
我正在尝试自动生成我的数据库(如果它不存在)并且 运行 Seed()
方法来填充数据。在我的数据库上下文构造函数中,我有这个:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
效果很好,我的数据库自动创建了我想要的所有表,但似乎没有调用 Seed()
方法,我的数据库是空的。这是我的 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 = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
base.Seed(context);
}
}
当我在 Nuget
控制台中 运行 Update-Database
时,数据会在数据库创建后填充,但是 MigrateDatabaseToLatestVersion
不会调用 Seed()
方法.
会发生什么?我尝试手动 运行ning 迁移,取自 here:
var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);
var migrator = new DbMigrator(configuration);
migrator.Update();
但还是不行。
编辑:
好的,经过更多的测试我发现 Seed()
方法 运行s 但只有当数据库已经存在时,即在第一个 运行 数据库是第一次创建 Seed()
方法没有执行,但是当我 运行 我的应用程序第二次 Seed()
被执行时。我还必须添加 context.SaveChanges()
才能使其正常工作(感谢评论中的@DavidG):
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
context.SaveChanges();
base.Seed(context);
}
所以也许我可以在 Configuration()
中手动调用 Seed()
并进行一些检查以避免添加重复数据或修改已经存在的数据。
我得到了这个 Configuration
class:
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
class:
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; }
}
我正在尝试自动生成我的数据库(如果它不存在)并且 运行 Seed()
方法来填充数据。在我的数据库上下文构造函数中,我有这个:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
效果很好,我的数据库自动创建了我想要的所有表,但似乎没有调用 Seed()
方法,我的数据库是空的。这是我的 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 = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
base.Seed(context);
}
}
当我在 Nuget
控制台中 运行 Update-Database
时,数据会在数据库创建后填充,但是 MigrateDatabaseToLatestVersion
不会调用 Seed()
方法.
会发生什么?我尝试手动 运行ning 迁移,取自 here:
var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);
var migrator = new DbMigrator(configuration);
migrator.Update();
但还是不行。
编辑:
好的,经过更多的测试我发现 Seed()
方法 运行s 但只有当数据库已经存在时,即在第一个 运行 数据库是第一次创建 Seed()
方法没有执行,但是当我 运行 我的应用程序第二次 Seed()
被执行时。我还必须添加 context.SaveChanges()
才能使其正常工作(感谢评论中的@DavidG):
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
context.SaveChanges();
base.Seed(context);
}
所以也许我可以在 Configuration()
中手动调用 Seed()
并进行一些检查以避免添加重复数据或修改已经存在的数据。
我得到了这个 Configuration
class:
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
class:
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; }
}