简体   繁体   English

使用Entity-Framework中更新的DatabaseGeneratedOption迁移实体

[英]Migrate entity with updated DatabaseGeneratedOption in Entity-Framework

I have created code-first app according to this article - Code First to a New Database . 我根据这篇文章创建了代码优先的应用程序 - Code First to a New Database Now I am going to change DatabaseGeneratedOption for Blog.BlogId . 现在我要为Blog.BlogId更改DatabaseGeneratedOption。 I changed my code in next way: 我以下一种方式更改了我的代码:

public class Blog
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int BlogId
    {
        get;set;
    }
    ...
}

And created migration for this code update: 并为此代码更新创建了迁移:

public override void Up()
{
    DropForeignKey("dbo.Posts", "BlogId", "dbo.Blogs");
    DropPrimaryKey("dbo.Blogs");
    AlterColumn("dbo.Blogs", "BlogId", c => c.Int(nullable: false, identity: false));
    AddPrimaryKey("dbo.Blogs", "BlogId");
    AddForeignKey("dbo.Posts", "BlogId", "dbo.Blogs", "BlogId", cascadeDelete: true);
}

According to this I changed code that created blog entity in Main function (added BlogId there.) 根据这个我更改了在Main函数中创建博客实体的代码(在那里添加了BlogId 。)

var blog = new Blog
{
    Name = name,
    BlogId = 110//it could be any other value that isn't represented in column
};

Now when I am trying to run my code I am getting next exception: DbUpdateException with next message - Cannot insert explicit value for identity column in table 'Blogs' when IDENTITY_INSERT is set to OFF. 现在,当我尝试运行我的代码时,我得到下一个异常:带有下一条消息的DbUpdateException - 当IDENTITY_INSERT设置为OFF时,无法在表'博客'中为标识列插入显式值。

From other hand when I delete all migrations and create initial migration from updated entity and creating db without identity flag (don't trying to update exist db), my code that create entity with my BlogId works. 另一方面,当我删除所有迁移并从更新的实体创建初始迁移并创建没有身份标记的数据库(不要尝试更新存在的数据库)时,我的代码使用我的BlogId创建实体。

My issue that in real project I have created table and I don't wont to recreate entire table just wont to update key column. 我的问题是在实际项目中我创建了表,我不会重新创建整个表只是不会更新键列。 How to do it with entity framework migration? 如何使用实体框架迁移?

You're trying to drop an IDENTITY property from a column, and unfortunately, that's usually not trivial (and for SQL Server at least, I don't think it's possible). 您试图从列中删除IDENTITY属性,不幸的是,这通常不是微不足道的(至少对于SQL Server,我认为这不可能)。

For some explanation, see: 有关解释,请参阅:


The last few links also provide some ideas for how you can customize your migration to remove the IDENTITY column and still keep your data. 最后几个链接还提供了一些有关如何自定义迁移以删除IDENTITY列并仍然保留数据的建议。 For example, from the last link: 例如,从最后一个链接:

The steps for changing the identity setting on a column in SQL Server are: 在SQL Server中更改列的标识设置的步骤如下:

  1. Drop all foreign key constraints that point to the primary key we are changing 删除指向我们正在更改的主键的所有外键约束
  2. Drop the primary key constraint 删除主键约束
  3. Rename the existing column (so that we can re-create the foreign key relationships later) 重命名现有列(以便我们以后可以重新创建外键关系)
  4. Add the new primary key column with the new identity setting 使用新标识设置添加新主键列
  5. Update existing data so that previous foreign key relationships remain 更新现有数据,以便保留以前的外键关系
  6. If the new column is an identity column, we need to update all foreign key columns with the new values 如果新列是标识列,则需要使用新值更新所有外键列
  7. If the new column doesn't have identity on, we can copy the old values from the previous identity column 如果新列没有标识,我们可以复制先前标识列中的旧值
  8. Drop old primary key column 删除旧的主键列
  9. Add primary key constraint 添加主键约束
  10. Add back foreign key constraints 添加外键约束

http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/ http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM