Scaffold migration with context key change

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

Question

My project was reorganized, and as a result, the database context's namespace name and related Code First configuration were changed. My database's "InitialCreate" scaffolded migration was the only one I had at that moment, so__MigrationHistory table has one row with some data in it.MigrationId and aContextKey providing the name of the class and namespace where theConfiguration class.

Executing after I've moved things aroundGet-Migrations yielded no results following the modification ofContextKey The "InitialCreate" migration was appropriately enumerated in accordance with my colleague's advice.

What actions should I have taken during the adjustments to ensure that my migrations would continue without interruption and avoid the need to rename the files?ContextKey through hand? That's obviously not a significant concern for a single applied migration, but doing that for dozens of applied migrations would be a major hassle.

1
7
1/25/2017 7:44:40 AM

Accepted Answer

ZZZ_tmp
14
5/23/2017 10:31:20 AM

Popular Answer

I had two separate database projects, one comprising a model and mapping, the other only a migration setup, and both projects had all migrations. I combined the migrations-containing project with the model-containing project, etc.

By doing the following actions, I was able to finally resolve the issue of being unable to create new migrations, maintain database updates, or migrate schema to a prior state.

  • I modified the namespaces in the merged project to reflect the new namespace value in all migrations and configuration.
  • I ran the following query on the database:

    USE [DatabaseName]
    
    GO
    
    UPDATE [dbo].[__MigrationHistory]
       SET [ContextKey] = N'NewNamepacePlusConfigurationClassName'      
     WHERE ContextKey= N'NamepacePlusConfigurationClassName'
    
    GO
    
  • I create projects with migrations and setup.

Now everything functions as intended, and I can even use statement to change the schema backwards.

Update-Database -TargetMigration PreviousMigration






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