title: Entity Framework Core 2.1 Chaining OnModelBuilder HasKey and HasData
body: <p>I'm trying to seed my database when I create my models and coincidentally one of my tables(UserRoles) has keys in both of their columns. When I place a the HasData the next row after the HasKey and run the add-migration command, I get this error:</p>
  
  <blockquote>
    <p>An attempt was made to use the model while it was being created. A
    DbContext instance cannot be used inside OnModelCreating in any way
    that makes use of the model that is being created.</p>
  </blockquote>
  
  <p>If I remove the HasKey or HasData, the seeding goes on as per normal. 
  What is the proper way to seed data for that table while also defining both columns as keys?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>modelBuilder.HasDefaultSchema(schema: DatabaseGlobals.SchemaName);
  modelBuilder.Entity&lt;Applications&gt;().HasData(application);
  modelBuilder.Entity&lt;ApplicationUsers&gt;().HasData(user);
  modelBuilder.Entity&lt;Roles&gt;().HasData(role);
  modelBuilder.Entity&lt;UserRoles&gt;().HasData(UserRoles);
  modelBuilder.Entity&lt;UserRoles&gt;().HasKey(k=&gt;new { k.UserId, k.RoleId});
  modelBuilder.Entity&lt;IdentityUserRole&lt;string&gt;&gt;().HasKey(k =&gt; new { k.UserId, k.RoleId });
  modelBuilder.Entity&lt;EventLogs&gt;();
  base.OnModelCreating(modelBuilder);
  </code></pre>
  
  <p>If I combined HasKey and HasData, this is the error I get:</p>
  
  <blockquote>
    <p>KeyBuilder' does not contain a definition for 'HasData'</p>
  </blockquote>
  
  <p>EDIT: Added full error</p>
  
  <pre><code>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.InvalidOperationException: An attempt was made to use the model while it was being created. A DbContext instance cannot be used inside OnModelCreating in any way that makes use of the model that is being created.
     at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
     at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
     at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
     at Microsoft.EntityFrameworkCore.DbContext.get_Model()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
     --- 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.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
     at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.GetData(Boolean providerValues)
     at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateData(IModel model)
     at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model)
     at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)
     at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model)
     at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
     at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
     at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
     at System.Lazy`1.CreateValue()
     at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
     at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope scope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
     at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
     at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
     at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
     at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
     at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
     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.OperationBase.&lt;&gt;c__DisplayClass3_0`1.&lt;Execute&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  Exception has been thrown by the target of an invocation.
  </code></pre>