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

0

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

I did the procedure to rename the Street to Address property in the class and after I ran the Add-Migration Name-Street-to-Address command, the Entity Framework generated a class containing the change that will be performed, but I see that a Drop in Column will happen instead of Renaming.

I would like to know what procedure I should do to perform 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");
 }
    
asked by anonymous 15.10.2017 / 14:41

1 answer

2

Change your settings file to this:

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

 public override void Down()
 {
   RenameColumn("dbo.Empresas", "Endereco", "Rua");
 }
    
15.10.2017 / 16:05