To change the column name of a table using the Add-Migration Code First Asp.Net MVC command

Asked

Viewed 649 times

0

I have a table already created and I want to change the column name without them being deleted, because it already has a lot of information.

I did the procedure of renaming the property Street to Address in class and then executed the command Add-Migration Change-Name-Street-to-Address so the Entity Framework generated a class containing the change that will be made, but I see that there will be a Drop in the Column instead of Rename.

I would like to know what procedure I should do to carry out this maintenance.

Code below

public override void Up()
{
  AddColumn("dbo.Empresas", "Endereco", c => c.String(nullable: false, maxLength: 100));
  DropColumn("dbo.Empresas", "Rua");
 }

 public override void Down()
 {
    AddColumn("dbo.Empresas", "Rua", c => c.String(nullable: false, maxLength: 100));
    DropColumn("dbo.Empresas", "Endereco");
 }

1 answer

2


Change your settings file for this:

public override void Up()
{
 RenameColumn("dbo.Empresas", "Rua", "Endereco");
 }

 public override void Down()
 {
   RenameColumn("dbo.Empresas", "Endereco", "Rua");
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.