Can I rename a Code First Migration in ASP.NET MVC 5

3.2k views Asked by At

This is a slightly odd question about code first migrations in asp.net mvc 5. I have an on going project and I use manual migrations to update my database. If I do anything that changes the database structure in my model, I then go into the package manager console and type

add-migration "MigrationName"

but along the way I have named two of my migrations the wrong name. I'd like to correct the name on each, but I want to make sure this will not affect the integrity of them, I only plan to rename the second portion of the file name (The descriptive part) not the timestamp.

For Instance I had the following name:

201411190809335_CreateStageTypeRender

but it should have read

201411190809335_CreateSeatingTypeRender

if I don't change the name now in the future I might target that migration on accident because I named it wrong.

It seems that when I have renamed them before in cases where it wasn't an important project, that visual studio renamed any subfiles (.cs, .resx). SO it would seem that this would not be a problem, plus the timestamp will be staying in pace so that should keep them in order.

Question: Is there anything else I should take into consideration. Is it ok to rename the descriptive part of the migration filename?

BTW.. I can also go inside the Designer.cs file and rename the get return value from:

public sealed partial class CreateStageTypeRender : IMigrationMetadata
    private readonly ResourceManager Resources = new ResourceManager(typeof(CreateStageTypeRender));
    string IMigrationMetadata.Id
    {
        get { return "201411190809335_CreateStageTypeRender"; }
    }

to

public sealed partial class CreateSeatingTypeRender : IMigrationMetadata
    private readonly ResourceManager Resources = new ResourceManager(typeof(CreateSeatingTypeRender));
    string IMigrationMetadata.Id
    {
        get { return "201411190809335_CreateSeatingTypeRender"; }
    }

Finally I have gone into the database and renamed the records that correspond with the old migration names and changed them to reflect the new migration names. Otherwise I assume that EF/Visual Studio would think that there are new migrations when there are not.

I'm thinking I have all my bases covered. Please let me know if you think I have missed anything or if this is not a good idea for some reason.

1

There are 1 answers

0
Michael Adamission On

I finally got back around to the project that had this issue and it works great.

In your example, you mention that you change the return type to CreateSeatingTypeRender... I just want to clarify that, for my testing, in the name of consistency, there are five places that the change should be made.

Four of those are in code: the class name, the .designer file's partial class name, the resource manager typeof and the IMigrationMetadata.Id property you mentioned…

The final place is in the data, as you also mentioned, update the MigrationId, from the old name to the new name.

Making those changes the upgrade-database command worked perfectly.

For anyone trying this, also, keep in mind if you're renaming a migration on your dev database... if that migration already exists on a production machine, you'll have to manually rename the __migration table rows there as well and any other places that that migration has already made it out to when you do a deploy to that machine.

Testing Notes: • I tested on ef 6.1.3 • This is not MVC5 specific