3
我使用EntityFramework 5与代码优先迁移。我想要做的是根据构建配置指定不同的种子数据。例如:EntityFramework迁移 - 每构建配置播种
protected override void Seed(EFDbContext context)
{
// This will be run in every Build configuration
context.Table1.AddOrUpdate(new Table1 { Field1 = "Foo", Field2 = "Bar" });
#if DEBUG
// This will only be executed if we are Debuging
context.Table1.AddOrUpdate(new Table1 { Field1 = "Blah", Field2 = "De Blah" });
context.Table2.AddOrUpdate(new Table2 { Field1 = "Development Only" });
}
我知道代码也许是错误的,我可以找出正确的方法调用一次,我知道采取这样做的最佳途径。
EF有没有内置的方法,我错过了做这种事情?
感谢
UPDATE
我到底使用的代码是:
protected override void Seed(EFDbContext context)
{
// This will be run in every Build configuration
context.Table1.AddOrUpdate(t = t.Field1, new Table1 { Field1 = "Foo", Field2 = "Bar" });
#if DEBUG
// This will only be executed if we are Debuging
context.Table1.AddOrUpdate(t = t.Field2, new Table1 { Field1 = "Blah", Field2 = "De Blah" });
context.Table2.AddOrUpdate(t = t.Field1, new Table2 { Field1 = "Development Only" });
#endif
}
这是为雅尼克说,但在AddOrUpdate方法,你需要在传递EF将用它来确定它是否为新条目。不是我的问题的一部分,但认为我应该提供正确的方法供将来参考。