-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting pending model changes with EF Core 5 (in EF Core Power Tools) does not work #21953
Comments
I tried:
But then I got:
(and a database provider IS configured) |
@ErikEJ But if you don't need to process old snapshots a simpler way to do this would be var dependencies = context.GetService<ProviderConventionSetBuilderDependencies>();
var relationalDependencies = context.GetService<RelationalConventionSetBuilderDependencies>();
var typeMappingConvention = new TypeMappingConvention(dependencies);
typeMappingConvention.ProcessModelFinalizing(((IConventionModel)modelSnapshot.Model).Builder, null);
var relationalModelConvention = new RelationalModelConvention(dependencies, relationalDependencies);
var sourceModel = relationalModelConvention.ProcessModelFinalized(snapshot.Model);
var modelDiffer = context.GetService<IMigrationsModelDiffer>();
var hasDifferences = modelDiffer.HasDifferences(
((IMutableModel)sourceModel).FinalizeModel().GetRelationalModel(),
context.Model.GetRelationalModel()); |
OMG - thanks, I will give it a try 😄 |
@AndriySvyryd Why do I need this 2 unused variables??
|
You don't need to keep them after the conventions have run. |
I would say "simpler way" is a strong word (compared to the EF Core 3 solution) |
I must be doing something wrong - unsure about the references to modelSnapshot and snapshot variables in your sample. Now I am getting:
Running this code:
|
I WAS doing it wrong - ended up with this, which appears to work to some extent: var dependencies = context.GetService<ProviderConventionSetBuilderDependencies>();
var relationalDependencies = context.GetService<RelationalConventionSetBuilderDependencies>();
var hasDifferences = false;
if (migrationsAssembly.ModelSnapshot != null)
{
var typeMappingConvention = new TypeMappingConvention(dependencies);
typeMappingConvention.ProcessModelFinalizing(((IConventionModel)migrationsAssembly.ModelSnapshot.Model).Builder, null);
var relationalModelConvention = new RelationalModelConvention(dependencies, relationalDependencies);
var sourceModel = relationalModelConvention.ProcessModelFinalized(migrationsAssembly.ModelSnapshot.Model);
hasDifferences = modelDiffer.HasDifferences(
((IMutableModel)sourceModel).FinalizeModel().GetRelationalModel(),
context.Model.GetRelationalModel());
}
var pendingModelChanges = (!databaseExists || hasDifferences); |
@AndriySvyryd Thanks for your help, btw! |
I was comparing it to the |
Trying to get pending model changes fails with error - I am probably doing something wrong!
System.InvalidOperationException: The database model hasn't been initialized. The model needs to be finalized before the database model can be accessed.
at Microsoft.EntityFrameworkCore.RelationalModelExtensions.GetRelationalModel(IModel model)
Steps to reproduce
Further technical details
EF Core version: 5, preview 6
Database provider: Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET Core 3.1
The text was updated successfully, but these errors were encountered: