实体框架 - 迁移 - 代码优先 - 每次迁移播种 [英] Entity Framework - Migrations - Code First - Seeding per Migration
问题描述
我正在研究迁移以清理我们的部署过程.将变更推送到生产时所需的人工干预越少越好.
I am looking into Migrations in an effort to clean up our deployment processes. The less manual intervention required when pushing a change to production the better.
我遇到了迁移系统的 3 个主要障碍.如果我想不出一个干净的方法来绕过它们,它们就会被阻止.
I have run into 3 major snags with the migrations system. They are show stoppers if I can not figure out a clean way around them.
1.如何为每次迁移添加种子数据:
我执行命令add-migration",该命令使用 Up 和 Down 功能构建一个新的迁移文件.现在,我想通过 Up 和 Down 更改自动更改数据.我不想将 Seed 数据添加到 Configuration.Seed 方法中,因为这会针对所有以各种重复问题结束的迁移运行.
I execute the command "add-migration" which scaffolds a new migration file with Up and Down functions. Now, I want to automatically make changes to the data with both Up and Down changes. I don't want to add the Seed data to the Configuration.Seed method as this runs for all migrations which ends in all sorts of duplication problems.
2.如果以上都不可能,我该如何避免重复?
我有一个枚举,我通过它循环将值添加到数据库中.
I have an enum that I loop through to add the values to the database.
foreach(var enumValue in Enum.GetValues(typeof(Access.Level)))
{
context.Access.AddOrUpdate(
new Access { AccessId = ((int)enumValue), Name = enumValue.ToString() }
);
}
context.SaveChanges();
即使我使用 AddOrUpdate,我仍然在数据库中得到重复项.上面的代码让我想到了我的第三个也是最后一个问题:
Even though I am using AddOrUpdate, I still get duplicates in the database. The above code brings me to my 3rd and final problem:
3.我怎样才能播种主键?
我上面代码的枚举是:
public class Access
{
public enum Level
{
None = 10,
Read = 20,
ReadWrite = 30
}
public int AccessId { get; set; }
public string Name { get; set; }
}
我指定了我想要作为主键的值,但实体框架似乎忽略了它.他们最终仍然是 1,2,3.我如何使它成为 10、20、30?
I am specifying the values that I want as my primary key, but Entity Framework seems to ignore it. They still end up being 1,2,3. How do I get it to be 10,20,30?
这些 EF 的限制是目前还是有意限制以防止我没有看到的其他类型的灾难?
Are these limitations of EF at the moment or are they intentional constraints to prevent some other kind of catastrophe I am not seeing?
推荐答案
- 当我有想要通过迁移插入的固定数据时,我使用对
Sql("Insert ...")
的调用将插入内容直接放入 Up() 迁移中.请参阅本页中间的注释:如何插入固定数据 - 您可以通过调用 AddOrUpdate 重载来防止 Seed 方法中的重复,该重载采用指定自然键的标识符表达式 - 请参阅此答案 和 此博客条目一>.
- 默认情况下,整数主键被创建为身份字段.要以其他方式指定,请使用
[DatabaseGenerated(DatabaseGeneratedOption.None)]
属性
- When I have fixed data that I want to insert with a migration, I put the inserts directly in the Up() migration using calls to
Sql("Insert ...")
. See the note halfway down this page: how to insert fixed data - You prevent duplicates in the Seed method by calling the AddOrUpdate overload that takes an identifier expression specifying the natural key - see this answer and this blog entry.
- Primary keys that are integers are created as identity fields by default. To specify otherwise use the
[DatabaseGenerated(DatabaseGeneratedOption.None)]
attribute
我认为这是对 Initializer 和 Seed 方法
以下是如何使用 AddOrUpdate 方法的示例:
Here is an example of how to use the AddOrUpdate method:
foreach(var enumValue in Enum.GetValues(typeof(Access.Level)))
{
context.Access.AddOrUpdate(
x => x.Name, //the natural key is "Name"
new Access { AccessId = ((int)enumValue), Name = enumValue.ToString() }
);
}
这篇关于实体框架 - 迁移 - 代码优先 - 每次迁移播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!