[英]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: 有关解释,请参阅:
Entering keys manually with Entity Framework 使用Entity Framework手动输入密钥
EF puts this in the migration:
EF将此放入迁移中:
AlterColumn("dbo.Events", "EventID", c => c.Int(nullable: false, identity: false))
And the sql generated is this:
而生成的sql是这样的:
ALTER TABLE [dbo].[Events] ALTER COLUMN [EventID] [int] NOT NULL`
Which actually does diddly squat.
这实际上确实蹲了下来。
https://dba.stackexchange.com/questions/87420/how-to-remove-identity-specification-for-multiple-tables https://dba.stackexchange.com/questions/87420/how-to-remove-identity-specification-for-multiple-tables
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中更改列的标识设置的步骤如下:
- Drop all foreign key constraints that point to the primary key we are changing
删除指向我们正在更改的主键的所有外键约束
- Drop the primary key constraint
删除主键约束
- Rename the existing column (so that we can re-create the foreign key relationships later)
重命名现有列(以便我们以后可以重新创建外键关系)
- Add the new primary key column with the new identity setting
使用新标识设置添加新主键列
- Update existing data so that previous foreign key relationships remain
更新现有数据,以便保留以前的外键关系
- If the new column is an identity column, we need to update all foreign key columns with the new values
如果新列是标识列,则需要使用新值更新所有外键列
- If the new column doesn't have identity on, we can copy the old values from the previous identity column
如果新列没有标识,我们可以复制先前标识列中的旧值
- Drop old primary key column
删除旧的主键列
- Add primary key constraint
添加主键约束
- 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.