3

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?

Cataklysim
  • 637
  • 6
  • 21
  • 1
    Are you sure that migration updated valid database? Maybe you use other connection string than the update-database. – pwas Dec 06 '16 at 07: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 06 '16 at 07: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... – Christoph Fink Dec 06 '16 at 07: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 06 '16 at 07:20

1 Answers1

11

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.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • Worked. Now i just need to take care of that two identical migration files. But thank you very much. – Cataklysim Dec 06 '16 at 07: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... – Christoph Fink Dec 06 '16 at 07: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. – starmandeluxe Nov 01 '17 at 05: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! – starmandeluxe Nov 02 '17 at 02: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)... – Christoph Fink Nov 02 '17 at 08:22
  • Fact is, some people are using EF Core and it's better to let them know about these potential gotchas. I don't see how reverting all migrations is helpful just to get a property's name changed, especially if some of those migrations drop tables and then you could have data lost. – starmandeluxe Nov 02 '17 at 08:56
  • @starmandeluxe Sorry if I wasn't clear, but I did not mean "undo the migration", but "undo the changes to the migration" (hence the reference to source control). But I can't talk about stuff I do not know - I simply never worked with EF Core yet... – Christoph Fink Nov 02 '17 at 09:29
  • @starmandeluxe ...but I now checked it quickly with EF Core: It is the same there as with EF - there are now references to the entities itself in the migration (which actually makes sense - how would you delete an entity otherwise?) -> it should work with EF Core the same way as with EF. – Christoph Fink Nov 02 '17 at 09:31
  • Exactly my problem. Exactly the solution that solved it. Thank you! Rename() did the trick. – nrod Mar 29 '23 at 15:27