Database 未调用实体框架种子方法
我们正在使用EntityFramework4.4和迁移。数据库已经存在,我们需要定期更新它。但是,不会调用seed方法,因此不会添加查找值 代码如下所示:Database 未调用实体框架种子方法,database,entity-framework,asp.net-mvc-4,c#-4.0,code-first,Database,Entity Framework,Asp.net Mvc 4,C# 4.0,Code First,我们正在使用EntityFramework4.4和迁移。数据库已经存在,我们需要定期更新它。但是,不会调用seed方法,因此不会添加查找值 代码如下所示: internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext> { public Configuration() { AutomaticMigrationsEnabled = false
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; }
}
但移民却空空如也
有人知道为什么会调用种子cmot以及为什么没有检测到查找表中的附加值吗
谢谢
N迁移种子方法 在执行更新数据库PowerShell命令时运行 执行 您需要调用
updatedatabase
notaddmigration
添加迁移
构建一个迁移文件,其中包含将数据库迁移到新版本的命令。它是空的,因为没有要进行的架构更改。如果您只想进行种子设定,则在调用更新数据库
之前无需调用添加迁移
参考资料:
谢谢你,这很有效:)。但是,有没有办法获取用于种子设定的sql脚本?更新迁移会返回一包错误,因为在我的种子设定方法中,我使用了文件或其他逻辑实现
Add-Migration Seed