4

i renamed a column in my class. It looked like this.

public class clsClassForStackoverflow
{
   [Column(TypeName = "varchar"), StringLength(150)]
   public string Name { get; set; }

Because of a change in the structure i had to rename it. So i deleted that row and added the new propertie. After that i created a new migration file to prepare my database update and in the file he wanted to delete the old column and add the new one. Well i changed the drop to a rename because there is still data in the database. With a drop it would be gone.

The new migration file:

public partial class _100126_2 : DbMigration
{
    public override void Up()
    {
        RenameColumn("dbo.tbClassForStackoverflow", "ClassForStackoverflow_Name",   "ClassForStackoverflow_NewName");
    }

    public override void Down()
    {
        RenameColumn("dbo.tbClassForStackoverflow", "ClassForStackoverflow_NewName", "ClassForStackoverflow_Name");
    }
}

After launching the database update i looked into my class and the propertie didnt changed. Its still named "Name" and not "NewName". How can i change that without let EF thinking i deleted and added a column?

4
  • 1
    Are you sure that migration updated valid database? Maybe you use other connection string than the update-database.
    – pwas
    Dec 6, 2016 at 7:11
  • @pwas yes. The connection string leads to our development database. The table already got the right columns. Its only the class that somehow didnt update the propertie.
    – Cataklysim
    Dec 6, 2016 at 7:14
  • You did the change in the DB "manually" and not via the migration? Or what exactly is the problem now? I can't follow anymore... Dec 6, 2016 at 7:16
  • @ChrFin No, i changed the migration file. The old one wanted to drop the column "Name" and create the column "NewName". Because of data loss, i changed the migration file to just alter the column. I launched the update and the table renamed it, the class didnt. If i rename the propertie in the class and create a new migration file, it may try to drop the column again.
    – Cataklysim
    Dec 6, 2016 at 7:20

1 Answer 1

12

You need to do this change in the following order:

  1. Rename the Name property in your Entity to NewName
  2. Add a new migration to your project
  3. Change the migration to do the rename instead of the Drop/Create, BUT only change the content of the Up/Down method
  4. Compile and run the migration

This is how I did it several times already without problems. Important is to not touch the model snapshot associated with the migration when its created.

P.S.: If I remember correctly EF even detects renames if some (to me unknown) constraints are met, because I think it wasn't always necessary to even change the migration when doing a simple property/column rename.

9
  • Worked. Now i just need to take care of that two identical migration files. But thank you very much.
    – Cataklysim
    Dec 6, 2016 at 7:49
  • 1
    @Cataklysim: I would recommend to undo all changes (DB and code) to the point before doing the "wrong renaming" and then redoing it... Dec 6, 2016 at 7:51
  • Er, you can't simply rename the column in the entity class. You will likely get compile errors since you probably reference the old name in code and the DbContext class. Nov 1, 2017 at 5:43
  • @ChrFin That's not completely true...I already tried that and it renamed old EF Core Migration files which is exactly what we DON'T want! Dangerous to do that! Nov 2, 2017 at 2:59
  • @starmandeluxe This question was about EF not EF Core and there are no references in migrations (only strings). I did it a few times already and it worked fine every time. For EF Core I would simply "undo" the migrations (one reason to always use source control)... Nov 2, 2017 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.