在 Entity-Framework 中使用更新的 DatabaseGeneratedOption 迁移实体
Migrate entity with updated DatabaseGeneratedOption in Entity-Framework
我已经根据这篇文章创建了代码优先的应用程序 - Code First to a New Database。现在我要为 Blog.BlogId
更改 DatabaseGeneratedOption。我用下一种方式更改了我的代码:
public class Blog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int BlogId
{
get;set;
}
...
}
并为此代码更新创建了迁移:
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);
}
据此,我更改了在 Main 函数中创建博客实体的代码(在此处添加了 BlogId
。)
var blog = new Blog
{
Name = name,
BlogId = 110//it could be any other value that isn't represented in column
};
现在,当我尝试 运行 我的代码时,我得到下一个异常:DbUpdateException 和下一条消息 - 无法为标识插入显式值当 IDENTITY_INSERT 设置为关闭时 table 'Blogs' 中的列。
另一方面,当我删除所有迁移并从更新后的实体创建初始迁移并创建不带身份标志的数据库(不要尝试更新现有数据库)时,我使用 BlogId
创建实体的代码有效.
我的问题是在实际项目中我已经创建了 table 并且我不会重新创建整个 table 只是会更新键列。如何使用 entity framework 迁移?
您正试图从列中删除 IDENTITY 属性,不幸的是,这通常不是微不足道的(至少对于 SQL 服务器,我认为这是不可能的)。
有关解释,请参阅:
Entering keys manually with Entity Framework
EF puts this in the migration:
AlterColumn("dbo.Events", "EventID", c => c.Int(nullable: false, identity: false))
And the sql generated is this:
ALTER TABLE [dbo].[Events] ALTER COLUMN [EventID] [int] NOT NULL`
Which actually does diddly squat.
- Remove Identity from a column in a table
- http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/
最后几个 link 还提供了一些关于如何自定义迁移以删除 IDENTITY 列并仍然保留数据的想法。比如从最后的link:
The steps for changing the identity setting on a column in SQL Server are:
- 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/
我已经根据这篇文章创建了代码优先的应用程序 - Code First to a New Database。现在我要为 Blog.BlogId
更改 DatabaseGeneratedOption。我用下一种方式更改了我的代码:
public class Blog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int BlogId
{
get;set;
}
...
}
并为此代码更新创建了迁移:
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);
}
据此,我更改了在 Main 函数中创建博客实体的代码(在此处添加了 BlogId
。)
var blog = new Blog
{
Name = name,
BlogId = 110//it could be any other value that isn't represented in column
};
现在,当我尝试 运行 我的代码时,我得到下一个异常:DbUpdateException 和下一条消息 - 无法为标识插入显式值当 IDENTITY_INSERT 设置为关闭时 table 'Blogs' 中的列。
另一方面,当我删除所有迁移并从更新后的实体创建初始迁移并创建不带身份标志的数据库(不要尝试更新现有数据库)时,我使用 BlogId
创建实体的代码有效.
我的问题是在实际项目中我已经创建了 table 并且我不会重新创建整个 table 只是会更新键列。如何使用 entity framework 迁移?
您正试图从列中删除 IDENTITY 属性,不幸的是,这通常不是微不足道的(至少对于 SQL 服务器,我认为这是不可能的)。
有关解释,请参阅:
Entering keys manually with Entity Framework
EF puts this in the migration:
AlterColumn("dbo.Events", "EventID", c => c.Int(nullable: false, identity: false))
And the sql generated is this:
ALTER TABLE [dbo].[Events] ALTER COLUMN [EventID] [int] NOT NULL`
Which actually does diddly squat.
- Remove Identity from a column in a table
- http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/
最后几个 link 还提供了一些关于如何自定义迁移以删除 IDENTITY 列并仍然保留数据的想法。比如从最后的link:
The steps for changing the identity setting on a column in SQL Server are:
- 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/