C# 如何在不丢失数据的情况下重命名 Entity Framework 5 Code First 迁移中的数据库列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13104592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename a database column in Entity Framework 5 Code First migrations without losing data?
提问by Dante
I got the default ASP.NET MVC 4 template successfully running with EF 5.0 Code First Migrations. However, when I update a model property name, the corresponding table column data is dropped by EF 5.0.
我使用 EF 5.0 Code First Migrations 成功运行了默认的 ASP.NET MVC 4 模板。但是,当我更新模型属性名称时,EF 5.0 会删除相应的表列数据。
Is it somehow possible to rename the table column without dropping data in an automated way?
是否可以在不以自动方式删除数据的情况下重命名表列?
采纳答案by Josh Gallagher
Manually edit the Up and Down methods of the migration to use the RenameColumnmethod to replace the AddColumnand DropColumnthat it automatically generates for you.
手动编辑迁移的 Up 和 Down 方法以使用该RenameColumn方法来替换它AddColumn并DropColumn为您自动生成。
回答by Daniel Persson
Now, this answer is based on my knowledge of EF4.3, so I hope the migrations work roughly the same in EF5 :) After you've created a migration, you should be able to add code in the Up and Down methods, between the dropping of the old property and the creation of the new property. This code should move the property data in the correct direction. I've solved it with the SQL() method in where you can enter raw SQL to perform the data move.
现在,这个答案是基于我对 EF4.3 的了解,所以我希望迁移在 EF5 中的工作大致相同:) 创建迁移后,您应该能够在 Up 和 Down 方法中添加代码,在旧财产的丢弃和新财产的创建。此代码应将属性数据移动到正确的方向。我已经使用 SQL() 方法解决了这个问题,您可以在其中输入原始 SQL 来执行数据移动。
In the Up method of the migration:
在迁移的 Up 方法中:
SQL("update [TheTable] set [NewColumn] = [OldColumn]");
and in the Down() method:
在 Down() 方法中:
SQL("update [TheTable] set [OldColumn] = [NewColumn]");
The disadvantage of this approach is that you might couple your code with the database you're working with at the moment (since you're writing raw DB-specific SQL). There might be other methods available for data movement as well.
这种方法的缺点是您可能会将您的代码与您目前正在使用的数据库耦合(因为您正在编写原始数据库特定的 SQL)。可能还有其他方法可用于数据移动。
More info available here: MSDN
此处提供更多信息:MSDN
回答by jk7
Adding to Josh Gallagher's answer:
添加乔什加拉格尔的回答:
In some places the sp_RENAME syntax is described like this:
在某些地方,sp_RENAME 语法是这样描述的:
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'
However, that will actually include the brackets in the new column name.
但是,这实际上将包括新列名称中的括号。
DbMigration's RenameColumn() method will probably do the same, so avoid using brackets when specifying the new column name.
DbMigration 的 RenameColumn() 方法可能会执行相同的操作,因此在指定新列名时避免使用方括号。
Also, the auto-generated commands in Up() & Down() include DropPrimaryKey() and AddPrimaryKey() if the column being renamed is part of the primary key. These are not needed when using RenameColumn(). The underlying sp_RENAME automatically updates the primary key if needed.
此外,如果被重命名的列是主键的一部分,则 Up() 和 Down() 中自动生成的命令包括 DropPrimaryKey() 和 AddPrimaryKey()。使用 RenameColumn() 时不需要这些。如果需要,基础 sp_RENAME 会自动更新主键。
回答by womd
like +Josh Gallagher said, you can use up() for doing things like:
就像 +Josh Gallagher 所说的,你可以使用 up() 来做这样的事情:
public override void Up()
{
RenameColumn("dbo.atable","oldname","newname");
AddColumn("dbo.anothertable", "columname", c => c.String(maxLength: 250));
}
i've found thisa good help in gettin into migration ;)
我发现这对开始迁移很有帮助;)
回答by Zanon
As already said, replace the AddColumnand DropColumnthat is automatically generated with RenameColumn.
前面已经说了,更换AddColumn并DropColumn自动与生成RenameColumn。
Example:
例子:
namespace MyProject.Model.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class RenameMyColumn : DbMigration
{
public override void Up()
{
// Remove the following auto-generated lines
AddColumn("dbo.MyTable", "NewColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "OldColumn");
// Add this line
RenameColumn("dbo.MyTable", "OldColumn", "NewColumn");
}
public override void Down()
{
// Remove the following auto-generated lines
AddColumn("dbo.MyTable", "OldColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "NewColumn");
// Add this line
RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
}
}
}
回答by Jess
You canget the migration to call RenameColumnfor you if you do this:
如果你这样做,你可以让迁移RenameColumn为你打电话:
[Column("NewName")]
public string OldName { get; set; }
Here is the generated migration:
这是生成的迁移:
public override void Up()
{
RenameColumn(table: "Schema.MyTable", name: "OldName", newName: "NewName");
}
public override void Down()
{
RenameColumn(table: "Schema.MyTable", name: "NewName", newName: "OldName");
}
If you want your property and DB column to be the same name, you can rename the property later and remove the Columnattribute.
如果您希望您的属性和 DB 列具有相同的名称,您可以稍后重命名该属性并删除该Column属性。
回答by Th?o Frederic
you have 2 steps to rename column in code first migration
您有 2 个步骤来重命名代码第一次迁移中的列
- The first step,you add ColumnAttribute above your column which are changed, and then update-database command
- 第一步,在更改的列上方添加 ColumnAttribute,然后使用 update-database 命令
[Column("Content")]
public string Description { set; get; }
[Column("Content")]
public string Description { set; 得到; }
The second step,
add-migration yournamechange command in order to create a partial class DbMigration.
add into up and down method here
第二步,
add-migration yournamechange 命令以创建分部类 DbMigration。
在这里添加上下方法
RenameColumn("yourDatabase","name","newName");
RenameColumn("yourDatabase","name","newName");
public override void Up()
{
RenameColumn("dbo.your_database", "oldColumn",
"newColumn");
}
public override void Down()
{
RenameColumn("dbo.your_database", "newColumn",
"oldColumn");
}
Because when you connect, your database and model class will communicate via name_column at database and name_type at property method in model above.
因为当您连接时,您的数据库和模型类将通过上面模型中的数据库中的 name_column 和属性方法中的 name_type 进行通信。

