How to rename a database column in Entity Framewor

2019-03-08 09:24发布

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.

Is it somehow possible to rename the table column without dropping data in an automated way?

7条回答
啃猪蹄的小仙女
2楼-- · 2019-03-08 09:55

You can get the migration to call RenameColumn for you if you do this:

[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 Column attribute.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-08 10:00

like +Josh Gallagher said, you can use up() for doing things like:

public override void Up()
        {

            RenameColumn("dbo.atable","odlanem","newname");
            AddColumn("dbo.anothertable", "columname", c => c.String(maxLength: 250));

        }

i've found this a good help in gettin into migration ;)

查看更多
\"骚年 ilove
4楼-- · 2019-03-08 10:03

Manually edit the Up and Down methods of the migration to use the RenameColumn method to replace the AddColumn and DropColumn that it automatically generates for you.

查看更多
成全新的幸福
5楼-- · 2019-03-08 10:03

you have 2 steps to rename column in code first migration

  1. The first step,you add ColumnAttribute above your column which are changed, and then update-database command

[Column("Content")]
public string Description { set; get; }

  1. The second step,

    • add-migration yournamechange command in order to create a partial class DbMigration.

    • add into up and down method here

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.

查看更多
神经病院院长
6楼-- · 2019-03-08 10:07

Adding to Josh Gallagher's answer:

In some places the sp_RENAME syntax is described like this:

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.

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.

查看更多
老娘就宠你
7楼-- · 2019-03-08 10:10

As already said, replace the AddColumn and DropColumn that is automatically generated with 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");
        }
    }
}
查看更多
登录 后发表回答