ChristianHehlBM
ChristianHehlBM

Reputation: 71

Renaming DbContext class loses association with existing migrations/modelsnapshot

I have been developing an Application using Ef-Core for a while, which means i have multiple existing migrations (including the ModelSnapshot). Now i have decided to start using SqlLite for local development (while the hosted version still uses SqlServer). To reflect this in the code i created two more Context classes

SqlServerContext : AppContext {}

SqlLiteContext : AppContext {}

where AppContext contains all the Db-Sets while the derivated classes set up the different connections and some additional configurations (like a data-seed for local development). These get added via Dependency Injection in the startup, depending on the environment.

When i create a new Migration using

dotnet-ef migrations add TestMigration --context SqlServerContext

it creates a new ModelSnapshot (SqlServerContextModelSnapshot.cs) instead of continuing to use the existing one (AppContextModelSnapshot.cs). I have to specify the Context since migrations are dependent on the Databaseprovider being used.

Is there any way to tell my new Context to keep using the existing ModelSnapshot as a basis? Or am i using the context wrong and is there a better way to have environment specific databases and configurations?

Since the AppContext is already referenced everywhere in the project from the previous development, i would prefer not to simply rename the SqlServerContext to AppContext and the AppContext to something else (like BaseContext)

Upvotes: 4

Views: 751

Answers (1)

ChristianHehlBM
ChristianHehlBM

Reputation: 71

Ivan Stoev was right with his comment!

Just rename the AppContextModelSnapshot.cs to SqlServerContextModelSnapshot.cs and replace every appearence of [DbContext(typeof(AppContext))] with [DbContext(typeof(SqlServerContext))] inside the Snapshot and all the migrations.

Thanks!

Upvotes: 3

Related Questions