1

I'm learning .net core and try to figure out the best practices for migrations. I come from Django and when when I had made a change to the database I would just do:

First:
makemigrations which is responsible for creating new migrations based on the changes you have made to your models.

Then:
migrate which is responsible for applying migrations.

Now I try to translate this to .NET Core and look at migration-commands in the Package Manager Console. I assumed that the equivalent would be to run:

First:
Add-Migration -Name <myMigration> Adds a new migration.

Then:
Update-Database -Migration <myMigration> Updates the database to a specified migration.

But then I get:
There is already an object named '<modelName>' in the database.

I'm thinking that Add-Migration didn't just add the changes to the database I've made in the Models-folder but seem to want to add everything from the Models-folder. So what am I missing here? How to I make a migration that only apply the unique changes to the database?

4
  • You are likely already have this table created. Migrations only work, when you do it from scratch, when there are no existing tables there. Then first migration creates all tables, and the following migrations update it. The migrations files are always generated as a diff from the snapshot file in the Migration folder, not from the actual database! If all did this in the past, then there may be an __EfMigrationsHistory Table or something along the lines. This table contains an a list of entries like 20170621093328_Init. It tells which migrations have been already applied Commented Jun 21, 2017 at 7:34
  • Errors like yours can happen, when you apply a migration, then delete the 2017xxxxxxxxxxx_SomeName.cs file w/o doing Update-Database -Migration <previous_migraton_here>, then run Remove-Migration and then create a new migration. it's easy to mess up, especially if you can't wipe out the db and reseed it easily and don't use an version control system which allows you to roll back to a specific snapshot. Also calling Update-Database with -Migration isn't necessary if you want to update to the most recent version. Only for rollbacks or applying not the most recent one required Commented Jun 21, 2017 at 7:38
  • @Tseng I could easily wipe my database, but how would I do this? Would I just delete all the migrations with Remove-Migration -Name <migrationName>, then run Drop-Database, and go Add-Migration -Name Initial ? After that, would just Add-Migration work as intended? Commented Jun 21, 2017 at 7:43
  • Yes something along the lines of that. You can of course just drop the database and delete the whole Migrations folder to start from scratch. This should work as long as you never delete a migration file manually and never edit the snapshot file manually Commented Jun 21, 2017 at 7:45

1 Answer 1

1

You project is looking at a database that already exists. You may need to change you connection string / settings on the database your are pointing at.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.