Joh*_*ess 11 database entity-framework code-first c#-4.0 asp.net-mvc-4
我们正在使用Entity Framework 4.4并使用迁移.数据库已经存在,我们需要定期更新它.但是,种子方法未被调用,因此未添加查找值.
代码如下:
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SqlClient", new OurSqlServerMigrationSqlGenerator());
}
protected override void Seed(KinectionDbContext context)
{
SeedLookupTables(context);
}
private static void SeedLookupTables(KinectionDbContext context)
{
context.Titles.AddOrUpdate(t => t.Value,
new Title {Value = "Mr"},
new Title {Value = "Mrs"},
new Title {Value = "Miss"},
new Title {Value = "Ms"},
new Title {Value = "Dr"}
);
context.SaveChanges();
}
}
public class MyDbContext : ObjectContext
{
public MyDbContext()
{
}
static MyDbContext ()
{
Database.SetInitializer<KinectionDbContext>(null);
}
public DbSet<Title> Titles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我们打电话给:
Add-Migration Seed
Run Code Online (Sandbox Code Playgroud)
但迁移是空洞的.
有没有人知道为什么种子被调用cmot以及为什么没有检测到查找表中的其他值?
谢谢你
Col*_*lin 23
迁移种子方法
只要执行Update-Database PowerShell命令,就会运行
你需要调用Update-Database不Add-Migration
Add-Migration支持包含将数据库迁移到新版本的命令的迁移文件.它是空的,因为没有要进行的架构更改.如果你想做的只是种子,你不需要在打电话Add-Migration前打电话Update-Database
参考文献:
代码首先是Db初始化策略.
代码优先迁移建议阅读
托管迁移
数据库初始化程序和迁移种子方法