Efcore: preview7 - 'dotnet ef migrations remove' error 'The model must be finalized before GetTypeMapping can be used'

Created on 25 Jul 2019  路  13Comments  路  Source: dotnet/efcore

'dotnet ef migrations remove' - new error appeared after upgrading to preview7.
migrations are successfully removed, however.
And no errors on 'dotnet ef migrations add'

Removing migration '***********'.
System.InvalidOperationException: The model must be finalized before 'GetTypeMapping' can be used. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBuilder', that 'FinalizeModel' has been called.

   at Microsoft.EntityFrameworkCore.PropertyExtensions.GetTypeMapping(IProperty property)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsCodeGenerator.<>c.<GetNamespaces>b__15_1(IProperty p)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateSnapshot(String modelSnapshotNamespace, Type contextType, String modelSnapshotName, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
closed-fixed customer-reported type-bug

Most helpful comment

Still happening on 3.0.0-preview8.19405.11. New migration works perfectly however, any new migration or on migrations remove the issue appears.
The only option now is to drop the db and delete the migrations folder manually then add new migration.

All 13 comments

And it seems that ModelSnapshot is not reverted properly

I have the same problem with add migration...
When if i make new migration it's ok
But when i change something in the existing model i have the same exception

PM> add-migration Attaches -project MigrationMsSql MsSqlConnection Try to create ms sql option builder Try to create ms sql option builder Try to create role Try to create user account Try to create user role Try to create ms sql option builder MsSqlConnection Try to create ms sql option builder Try to create ms sql option builder System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The model must be finalized before 'GetTypeMapping' can be used. Ensure that either 'OnModelCreating' has completed or, if using a stand-alone 'ModelBuilder', that 'FinalizeModel' has been called. at Microsoft.EntityFrameworkCore.PropertyExtensions.GetTypeMapping(IProperty property) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimplePrincipalKeyValueFactory1..ctor(IProperty property)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.KeyValueFactoryFactory.CreateSimpleFactoryTKey
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.KeyValueFactoryFactory.CreateTKey
at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.<>c__191.<GetPrincipalKeyValueFactory>b__19_0(Key k) at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.GetPrincipalKeyValueFactoryTKey
at Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetPrincipalKeyValueFactoryTKey
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMapFactoryFactory.CreateFactoryTKey
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMapFactoryFactory.Create(IKey key)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.<>c.b__18_0(Key k)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitializedTParam,TValue
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.Microsoft.EntityFrameworkCore.Update.IUpdateEntry.set_EntityState(EntityState value)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IModel source, IModel target, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Exception has been thrown by the target of an invocation. PM>

@alexk8 @svfaust A couple of things. First, make sure you're using preview 7 versions of all .NET Core 3 related assets--specifically ASP.NET Core.

Second, we fixed an issue in this area recently, so please try with the nightly builds.

Finally, if this is still failing with the nightly builds, then please post a small, runnable project/solution or complete code listing that demonstrates the problem so we can fully investigate

@ajcvickers
yep, it was all preview7.
I've tried preview8 as you mentioned - and it's still actual issue
I hope I did everything right, please check
project - EFCore30Tests.zip
tooling:

>dotnet tool install --global dotnet-ef --version 3.0.0-preview8
Tool 'dotnet-ef' (version '3.0.0-preview8.19351.2') was successfully installed

I'm hitting this issue as well with two different projects.

If i try to call FinalizeModel manually at the end of OnModelCreating i get this exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel()
   at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__1()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
...

Downgrade of EF.Core to 3.0.0-preview6.19304.10 did work for me.
I had to downgrade Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore and Microsoft.AspNetCore.Identity.EntityFrameworkCore to 3.0.0-preview6.19307.2 as well.

@alexk8 @svfaust @LunicLynx I have been able to reproduce this--we will investigate. Thanks for reporting!

I'm not sure this is related. But for me the Add-Migration Cmdlet generated code for a table that already got a migration. When trying to remove this migration, the issue above did show up again.

Edit: Formatting, clarification

Given this is closed, what's the recommended "fix"? Upgrade to nightly builds?

@maartenba Yes, or wait for preview 8.

Still happening on 3.0.0-preview8.19405.11. New migration works perfectly however, any new migration or on migrations remove the issue appears.
The only option now is to drop the db and delete the migrations folder manually then add new migration.

Downgrade to preview6 still works. See https://github.com/aspnet/EntityFrameworkCore/issues/16737#issuecomment-515479316

@yamen769 There is a second bug in this area that shows up only when HasData is used in the model. See #17145

Was this page helpful?
0 / 5 - 0 ratings