How to re-create initial migration on the same .cs file

24,954

Solution 1

Here you can see how I solved this.

Be careful with this approach because in my scenario I'm still in development phase so I do not need to keep the database nor the data.

If you want to overwrite the current "InitialCreate" file and re-generate the DB, follow these steps:

1) Run in Package Manager Console:

Add-Migration InitialCreate -force

2) Delete the physical database (use SSMS, T-SQL or your prefered)

3) Run in Package Manager Console:

Update-Database -TargetMigration:0 | update-database -force | update-database -force

If you do not care about the current "InitialCreate" file itself, want to create a new one and re-generate the DB, follow these steps:

1) Delete current "InitialCreate" .cs file

3) Run in Package Manager Console:

Add-Migration InitialCreate

4) Delete the physical database (use SSMS, T-SQL or your prefered)

4) Run in Package Manager Console:

Update-Database -TargetMigration:0 | update-database -force | update-database -force

Solution 2

My Project is Solved.
Run in Package Manager Console:

  1. Drop-Database
  2. Remove-Migration
  3. Add-Migration InitialCreate
  4. update-database

Solution 3

Just delete the initial migration files from the 'Migrations' folder and retry.

Share:
24,954
Anon Dev
Author by

Anon Dev

Coder

Updated on July 05, 2022

Comments

  • Anon Dev
    Anon Dev almost 2 years

    As common in EF Code First I've generated an "Initial Create" migration file where is located an outdated model of my db (I'm developing the app so the model is still changing). Now I have a "new model" defined in my code and instead of creating a new migration for that I just want to update the already existing file, because it still is the initial create migration.

    I've tried using this without any luck

    Update-database -targetmigration $initialcreate

    It returns

    Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
    You can use the Add-Migration command to write the pending model changes to a code-based migration.
    

    I also tried this one, but it's always creating a new .cs file

    Add-Migration InitialCreate

    I'll appreaciate your help

  • Dinch
    Dinch over 4 years
    On my case, I had to right click to solution, and select Clean Solution menu item for some reason.
  • ashubuntu
    ashubuntu over 4 years
    @Dinch Thanks, saved my hour. I think what you did is necessary, otherwise in my case too, most of the suggestions here did not work.
  • ashubuntu
    ashubuntu over 4 years
    @Dinch also I have to run remove migration twice.
  • shelbypereira
    shelbypereira about 4 years
    I would suggest updating answer and adding @Dinch suggestion to Clean Solution (after performing Remove-Migration). I guess Visual studio is caching information in .pdb files or somewhere else. with addition of Clean Solution, this worked for me.