EF move migration to new project

c# ef-migrations entity-framework entity-framework-6

Question

I'm refactoring a project and want to move all the EF entities and the code-first migrations to a new project. I renamed the ContextKey in the _Migrations table to the new namespace. When running an Add-Migration, no new changes are detected (Up() and Down() are empty). But when I remove the localdb, the db isn't re-created (it did before the move). Apparently only migrations created after the move are run (but it shouldn't).

How can I make sure all migrations (also the ones before the move) are run when creating a new db?

--edit--

Never mind :(

I dragged and dropped the existing migrations to the new project and renamed the namespaces in the migration.cs files, but forgot the code behind migration.Designer.cs

1
2
4/29/2016 6:32:41 PM

Popular Answer

You can update all the ContextKey column values in the dbo._MigrationHistory table to match the new namespace and that's all.

For me i was moving all the code first models from ASP.NET MVC app to external Class library to share with other projects.

Below steps may help

  1. check the dbo._MigrationHistory and you can see all records have similar values which match the exact class of Configuration class
MyApp.Migrations.Configuration

2.(test step) run Update-Database from Package Manager Console with new class library selected and you will see for example below error

There is already an object named 'AspNetRoles' in the database.
  1. update all the records in the ContextKey column of _MigrationHistory table to match the new namespace
MyApp.Domain.Migrations.Configuration
3
4/11/2017 3:27:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow