实体框架 - 迁移 - 代码第一次 - 每迁移播种 [英] Entity Framework - Migrations - Code First - Seeding per Migration

查看:205
本文介绍了实体框架 - 迁移 - 代码第一次 - 每迁移播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待到迁移在努力清理我们的部署过程。推着改变生产更好时,少人工干预的需要。



我遇到了与迁移系统3大障碍。他们出现阻塞,如果我无法弄清楚他们周围清洁的方式。



1。我如何迁移每添加种子数据:



我执行命令加移民,这与脚手架上下功能的新的迁移文件。现在,我想自动更改与向上和向下变化的数据。我不想种子数据添加到Configuration.Seed方法,因为这会运行在各种重复的问题从而结束所有迁移。



2 。如果以上是不可能的,我怎么避免重复?



我有一个枚举,我通过循环的值添加到数据库中。

 的foreach(在Enum.GetValues​​ VAR enumValue(typeof运算(Access.Level)))
{
上下文。 Access.AddOrUpdate(
新的Access {ACCESSID =((INT)enumValue),名称= enumValue.ToString()}
);
}
context.SaveChanges();



虽然我使用AddOrUpdate,我仍然可以在数据库中重复。上面的代码引出了我的第三和最后一个问题:



3。 ?我怎样才能种子主键



我与上面的代码枚举是:



 公共类访问
{
公共枚举水平
{
无= 10,
读= 20,
读写= 30
}
公众诠释ACCESSID {搞定;组; }
公共字符串名称{;组; }
}



我指定我想作为我的主键的值,但实体框架似乎忽略它。他们最终仍是1,2,3。我如何得到它是10,20,30?



是EF的这些限制目前还是他们有意的限制,以防止其他某种灾难的我没有看到?


解决方案

  1. 当我有固定的,我想迁移到插入数据,我直接把插入使用呼叫 SQL中的向上()迁移(插入...)。见注中途下此页:如何插入固定数据

  2. 您防止种子的方法重复调用AddOrUpdate覆盖将标识符表达式,指定自然键 - 看到的这个答案这博客条目

  3. 主键是整数创建为默认的标识字段。要指定否则使用 [DatabaseGenerated(DatabaseGeneratedOption.None)] 属性



我觉得这是初始化函数和种子的方法



下面是如何使用AddOrUpdate方法的例子:

 的foreach(在Enum.GetValues​​ VAR enumValue(typeof运算(Access.Level)))
{
context.Access.AddOrUpdate(
X => x.Name,//自然键是名
新的Access {ACCESSID =((INT)enumValue),名称= enumValue.ToString()}
);
}


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.

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. How do I add Seed data per migration:

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. If the above is not possible, how do I avoid duplications?

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();

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. How can I seed Primary Keys?

My enumerable with the above code is:

public class Access
{
    public enum Level
    {
        None = 10,
        Read = 20,
        ReadWrite = 30
    }
    public int AccessId { get; set; }
    public string Name { get; set; }
}

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?

Are these limitations of EF at the moment or are they intentional constraints to prevent some other kind of catastrophe I am not seeing?

解决方案

  1. 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
  2. You prevent duplicates in the Seed method by calling the AddOrUpdate override that takes an identifier expression specifying the natural key - see this answer and this blog entry.
  3. Primary keys that are integers are created as identity fields by default. To specify otherwise use the [DatabaseGenerated(DatabaseGeneratedOption.None)] attribute

I think this is a good explanation of Initializer and Seed methods

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆