And*_*res 9 c# entity-framework ef-migrations
我正在尝试自动生成我的数据库(如果它不存在)并运行该Seed()方法来填充数据.在我的数据库上下文构造函数中,我有:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
Run Code Online (Sandbox Code Playgroud)
这很好用,我的数据库会根据需要自动创建所有表,但似乎Seed()没有调用该方法,我的数据库是空的.这是我的班级:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我Update-Database在Nuget控制台中运行时,数据在创建数据库后填充,但是不调用MigrateDatabaseToLatestVersion该Seed()方法.
可能发生什么?我尝试从这里手动运行迁移:
var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);
var migrator = new DbMigrator(configuration);
migrator.Update();
Run Code Online (Sandbox Code Playgroud)
但也行不通.
编辑:
好的,经过一些更多的测试后我发现该Seed()方法只在数据库已经存在时运行,也就是说,在第一次创建数据库时,第一次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);
}
Run Code Online (Sandbox Code Playgroud)
所以也许我可以手动调用Seed()内部Configuration()并进行一些检查,以避免添加重复数据或修改已存在的数据.
我最后得到了这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);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,通过这种方式,在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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4967 次 |
| 最近记录: |