如何更改EF代码第一次主键的名字吗? [英] How to Change the name of a primary key in EF Code First?
问题描述
我有一种情况,我想在实体更改主键名,并能够运行更新数据库-force。请参见下面的代码和错误让我试试。
I have a scenario where i would like to change the primary key name in an entity and be able to run update-database -force. See below for code and error getting when i try.
实体是:
public class Team
{
[Key]
[HiddenInput(DisplayValue = false)]
public virtual int Id { get; set; }
[Display(Name = "Full Name:")]
public virtual string Name { get; set; }
}
实体更改为:
public class Team
{
[Key]
[HiddenInput(DisplayValue = false)]
public virtual int TeamId { get; set; }
[Display(Name = "Full Name:")]
public virtual string Name { get; set; }
}
当我运行更新数据库-Force
我碰到下面的错误。为表'团队'指定
When i run Update-database -Force
i get the following error.
多重身份列。只有每桌一个标识列是允许的。
其命名约定的事情,我需要这是TeamId当我引用它后者,简单的ID冲突与儿童的实体类。
Its a matter of naming convention and i need this to be TeamId when i reference it latter, simply Id conflicts with child entity classes.
我如何能做到这一点任何想法成功?
Any ideas on how i can do this successfully?
推荐答案
依赖于EF的版本所使用。
即使迁移的结果,你将看到的是这样的:
Depends on the version of EF you are using. Even with migrations the result that you will see is something like:
删除列ID和添加列TeamId
"drop column Id " and "add column TeamId".
有了这个,你将失去所有的价值观和子女关系......
With this you will lose all values and "child connections"......
唯一的安全的解决方案,我此时看到的是迁移和手SQL操作的组合
The only "secure" solution I am seeing at this point is a mix of Migrations and "hand SQL operations".
简单的解决方法:
1 - 也要考虑到你已经有了一个基地迁移创建带有标识的表,如今创建一个更新的新移民。现在还没有运行。
1- taking in consideration that you already have a "base" migration creating the table with ID, now create the new migration with the "update". Now do NOT run it yet.
2 - 打开该文件,并写入生成的行之前新行,并使用SQL命令,像这样:
2- Open that file and write a new line BEFORE the generated lines and use a SQL command, something like this:
SQL("ALTER TABLE table_name RENAME COLUMN old_name to new_name;");
在迁移删除列这将更改名称并创建一个新的,会发生什么:你之前删除更改名称,然后删除被执行,但它将失败,但它不会伤害什么。
This will change the name BEFORE the migration deletes the column and create a new one, what will happen is: you change the name before the delete, then the delete is executed but it will "fail" but it will not hurt nothing.
但现在你会问:为什么我要这样做呢?好,如果您使用的是迁移,即使你删除行删除列,创建一个新的,下一次你自动创建一个新的迁移文件这一新的生产线将在那里......这是为什么。
But now you ask: why do I do this? well if you are using migrations even if you delete the lines to delete the column and create a new one, the next time you create automatically a new migration file this new lines will be there...... this is why.
修订解答#1
当我谈论实体框架迁移我指的是这样的:
的 http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
当你在运行程序包管理器控制台,一个新的文件添加迁移AddBlogUrl'命令(*的.cs)被创建。
When I talk about Entity Framework Migrations I am referring to this: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx When you Run the ‘Add-Migration AddBlogUrl’ command in Package Manager Console, a new file (*.cs) is created.
与SQL文件迁移文件的示例命令:
Example of this file migration file with SQL commands:
public partial class AddAbsencesTypesAndCategories : DbMigration
{
public override void Up()
{
CreateTable(
"pvw_AbsenceType",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
CountAsVacation = c.Boolean(nullable: false),
IsIncremental = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Id);
.....
AddColumn("pvw_Absence", "CategoryId", c => c.Int(nullable: false));
AddForeignKey("pvw_Absence", "StatusId", "pvw_AbsenceStatusType", "Id");
AddForeignKey("pvw_Absence", "CategoryId", "pvw_AbsenceType", "Id");
CreateIndex("pvw_Absence", "StatusId");
CreateIndex("pvw_Absence", "CategoryId");
DropColumn("pvw_Absence", "MainCategoryId");
DropColumn("pvw_Absence", "SubCategoryId");
......
Sql(@"
SET IDENTITY_INSERT [dbo].[pvw_AbsenceStatusType] ON
INSERT pvw_AbsenceStatusType (Id, Name) VALUES (1, N'Entwurf')
SET IDENTITY_INSERT [dbo].[pvw_AbsenceStatusType] OFF
");
.....
}
public override void Down()
{
........
}
这篇关于如何更改EF代码第一次主键的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!