问题描述
我们正在使用实体框架4.4,并使用迁移。该数据库已经存在,我们需要更新它定期。种子的方法,但是,不被调用和不被添加,因此查找值
We are using Entity Framework 4.4 and using migrations. The database already exists and we need to update it on regular basis. The seed method, however, is not being called and so lookup values are not being added.
在code看起来如下:
The code looks as follow:
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; }
}
和我们呼吁:
Add-Migration Seed
但迁移过来空。
有没有人有一个想法,为什么种子被调用CMOT,为什么没有被检测到查找表中的其他值?
Does anyone has an idea why the Seed is cmot being called and why the additional values in the lookup table are not being detected?
谢谢
ñ
推荐答案
的迁移方法种子
运行每当更新,数据库PowerShell命令是
执行
您需要调用更新,数据库
不是添加迁移
添加迁移
脚手架包含的命令来对数据库迁移到新版本的迁移文件。因为没有架构更改,使其为空。你并不需要调用添加迁移
之前调用更新 - 数据库
如果你想要做的是种子
Add-Migration
scaffolds a migration file containing commands to migrate the database to a new version. It is empty because there are no schema changes to make. You do not need to call Add-Migration
before calling Update-Database
if all you want to do is seed
参考文献:
<一个href=\"http://www.entityframeworktutorial.net/$c$c-first/database-initialization-strategy-in-$c$c-first.aspx\">$c$c第一DB初始化策略。结果
结果
结果
Database初始化函数和迁移种子的方法
这篇关于实体框架种子的方法不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!