Skip to content
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

InvalidOperationException when creating a migration and using HasData in OnModelCreating #17145

Closed
stevejgordon opened this issue Aug 14, 2019 · 2 comments · Fixed by #17206
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@stevejgordon
Copy link

I'm hitting an exception when creating a new migration on a DbContext which overrides OnModelCreating and includes HasData. I experienced this in an existing project which has been updated from 2,2 although I was able to reproduce the flow of events using a newly created ASP.NET Core 3.0 project.

Steps to reproduce

  1. Start with a default ASP.NET Core project which uses Identity
  2. Add a simple DbSet, I used...
public class ApplicationDbContext : IdentityDbContext
{
	public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
		: base(options)
	{
	}

	public DbSet<Thing> Things { get; set; }      

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Thing>()
			.HasData(new Thing { Id = 1, Name = "A thing" });

		base.OnModelCreating(modelBuilder);
	}
}

public class Thing
{
	public int Id { get; set; }
	public string Name { get; set; }
}
  1. Create a migration which should succeed
  2. Add another DbSet...
public class ApplicationDbContext : IdentityDbContext
{
	public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
		: base(options)
	{
	}

	public DbSet<Thing> Things { get; set; }
	public DbSet<AnotherThing> AnotherThings { get; set; }

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Thing>()
			.HasData(new Thing { Id = 1, Name = "A thing" });

		base.OnModelCreating(modelBuilder);
	}
}

public class Thing
{
	public int Id { get; set; }
	public string Name { get; set; }
}

public class AnotherThing
{
	public int Id { get; set; }
	public string Name { get; set; }
}
  1. Try to create a migration

The expected result is that a new migration is generated.
The actual result is an exception...

C:\Users\steve.gordon\source\repos\WebApplication3\WebApplication3>dotnet ef migrations add step3
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.0.0-preview8.19405.11 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
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.SimplePrincipalKeyValueFactory`1..ctor(IProperty property)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.KeyValueFactoryFactory.CreateSimpleFactory[TKey](IKey key)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.KeyValueFactoryFactory.Create[TKey](IKey key)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.<>c__19`1.<GetPrincipalKeyValueFactory>b__19_0(Key k)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.GetPrincipalKeyValueFactory[TKey]()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetPrincipalKeyValueFactory[TKey](IKey key)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMapFactoryFactory.CreateFactory[TKey](IKey key)
   --- 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.<get_IdentityMapFactory>b__18_0(Key k)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Key.get_IdentityMapFactory()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetIdentityMapFactory(IKey key)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetOrCreateIdentityMap(IKey key)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   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_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception has been thrown by the target of an invocation.

Further technical details

EF Core version: 3.0.0-preview8.19405.7 (although also tried with a nightly build of preview 9)
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 16.3 Preview 2.0

@ajcvickers ajcvickers self-assigned this Aug 14, 2019
@ajcvickers ajcvickers added this to the 3.0.0 milestone Aug 14, 2019
ajcvickers added a commit that referenced this issue Aug 16, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 16, 2019
ajcvickers added a commit that referenced this issue Aug 17, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
ajcvickers added a commit that referenced this issue Aug 17, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
ajcvickers added a commit that referenced this issue Aug 17, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
ajcvickers added a commit that referenced this issue Aug 17, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
ajcvickers added a commit that referenced this issue Aug 17, 2019
Fixes #17145 but see also #17205

The fix here allows the state manager to work, which is required when the model snapshot has model data in it.
@DibyodyutiMondal
Copy link

DibyodyutiMondal commented Aug 19, 2019

So we will have to wait for the general release for EF to work as expected again?

@ajcvickers
Copy link
Member

@DibyodyutiMondal It is fixed in the latest daily builds and will be included in preview 9.

@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview9 Aug 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview9, 3.0.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants