0

We have entity framework in our .net 4.6 project. We have been successfully running migrations for quite sometime now. All of a sudden when we ran migrations, it gave an error saying "Cannot create database ... as it already exists" . Not sure if its trying to start from scratch. I know the _migrationshistory table is populated. How it happened, probably because we created another .net core service with EF core migrations enabled on the same database but its a new context with completely new set of tables that don't overlap with the tables in our .net framework project where the migrations broke. So probably when we ran the migrations on the same database it reset something Can anybody guide us what can we do to have both migrations run in sync without impacting each other.As I said the tables dont overlap each other. There are two different migrationshistory tables created in the database for each project

Developer
  • 45
  • 6
  • 1
    You cannot have multiple contexts point to the same database. There can only be one _EFMigrationsHIstory table. Why don't you look into the table and see what the last migration applied is. – Canolyb1 Aug 02 '21 at 19:02
  • @Canolyb1 I see all the migrations in both the history tables (__EFMigrationsHistory and __MigrationHistory). But as I said when I run the migration it tries to create the database again – Developer Aug 02 '21 at 19:17
  • EFMigrationsHistory is for .net core, while MigrationsHistory is for Entity Framework pre .net core. If your project is for 4.6 then most likely you should be looking at the MigrationsHistory table and making sure all the migration ID's match up with your migration folder in your application. if even one ID is missing in the chain, EF will throw that error. – Canolyb1 Aug 02 '21 at 19:25
  • @Canolyb1. Correct. We have a .net framework service(__MigrationHistory) and a .net core service(__EFMigrationsHistory). Both our migrations table are in sync with our migration files in code. However when I run update-Database on our .net framework service, It tries to recreate the db and throws error because the db already exists). Why would it create the db again. I am not sure. My guess is when we ran than update-database for our dotnet core service, it messed up something causing the migrations to fail in the other. Because we havent had this issue before – Developer Aug 02 '21 at 19:33
  • Try running a script to see what script is returned. Update-Database -Verbose -f -Script – Canolyb1 Aug 02 '21 at 19:39
  • Yes I saw the script.Its trying to run every migration that has already been run . As I said the _MigrationsHistory table is already populated. So it seems the model snapshot is somehow reset. I tried adding a new migration and it errors out as well saying there are penging migrations – Developer Aug 02 '21 at 20:00

1 Answers1

0

You can only have one DbContext that uses Migrations. You can have additional DbContexts that map to the tables, but you have to manage the schema at the database or from a single DbContext that uses migrations.

To recover from the mess you're in I would remove the migrations entirely, and start over following the directions here.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67