-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
File a bug
I'm upgrading a working solution from .NET 6 to .NET 8 / EF Core 6 to EF Core 8. It's a complex solution with separate projects for Models and Migrations and a startup project to use with the migrations.
When I run Get-Migration...
PM> Get-Migration -Context XYZDbContext -Project XYZ2.Core.Infrastructure -StartupProject XYZ2.Core.EFDataApp -verbose
I get the following:
Unable to create a 'DbContext' of type ''. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
A colleague is trying to recreate the DbContext class to see where things go wrong. It appears from his testing that the problem is in SqlServerOnDeleteConventions.cs.
https://github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Metadata/Conventions/SqlServerOnDeleteConvention.cs
Include your code
I cloned some code from a Contoso University sample to try to reproduce the problem.
https://github.com/bgoodearl/ContosoU_dn8_EF
Unfortunately, the simpler solution works.
Include stack traces
Here's the sanitized verbose output of the complex project error with stack trace:
PM> Get-Migration -Context XYZDbContext -Project XYZ2.Core.Infrastructure -StartupProject XYZ2.Core.EFDataApp -verbose
Using project 'src\XYZ2.Core.Infrastructure'.
Using startup project 'src\XYZ2.Core.EFDataApp'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\bin\Debug\net8.0\XYZ2.Core.EFDataApp.deps.json --additionalprobingpath C:\Users\usr_x\.nuget\packages --runtimeconfig D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\bin\Debug\net8.0\XYZ2.Core.EFDataApp.runtimeconfig.json C:\Users\usr_x\.nuget\packages\microsoft.entityframeworkcore.tools\8.0.0\tools\netcoreapp2.0\any\ef.dll migrations list --json --context XYZDbContext --verbose --no-color --prefix-output --assembly D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\bin\Debug\net8.0\XYZ2.Core.Infrastructure.dll --project D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.Infrastructure\XYZ2.Core.Infrastructure.csproj --startup-assembly D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\bin\Debug\n
[Get-Migration_1_ContosoU.txt](https://github.com/dotnet/efcore/files/13531131/Get-Migration_1_ContosoU.txt)
[Get-Migration_1_ContosoU.txt](https://github.com/dotnet/efcore/files/13531133/Get-Migration_1_ContosoU.txt)
[Get-Migration_Sanitized_1.txt](https://github.com/dotnet/efcore/files/13531134/Get-Migration_Sanitized_1.txt)
et8.0\XYZ2.Core.EFDataApp.dll --startup-project D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\XYZ2.Core.EFDataApp.csproj --project-dir D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.Infrastructure\ --language C# --configuration Debug --working-dir D:\_dev\_XYZ\XYZ2_dn8 --root-namespace XYZ2.Core.Infrastructure --nullable
Using assembly 'XYZ2.Core.Infrastructure'.
Using startup assembly 'XYZ2.Core.EFDataApp'.
Using application base 'D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp\bin\Debug\net8.0'.
Using working directory 'D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.EFDataApp'.
Using root namespace 'XYZ2.Core.Infrastructure'.
Using project directory 'D:\_dev\_XYZ\XYZ2_dn8\src\XYZ2.Core.Infrastructure\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'XYZ2DbContextFactory'.
Found DbContext 'XYZDbContext'.
Finding application service provider in assembly 'XYZ2.Core.EFDataApp'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Finding DbContext classes in the project...
Using DbContext factory 'XYZ2DbContextFactory'.
Using context 'XYZDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type ''. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerOnDeleteConvention.<ProcessSkipNavigations>g__DefaultDeleteBehavior|6_1(IConventionSkipNavigation conventionSkipNavigation)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerOnDeleteConvention.ProcessSkipNavigations(IEnumerable`1 skipNavigations)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerOnDeleteConvention.ProcessEntityTypeAnnotationChanged(IConventionEntityTypeBuilder entityTypeBuilder, String name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation, IConventionContext`1 context)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnEntityTypeAnnotationChanged(IConventionEntityTypeBuilder entityTypeBuilder, String name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAnnotationChangedNode.Run(ConventionDispatcher dispatcher)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.DelayedConventionScope.Run(ConventionDispatcher dispatcher)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run()
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Dispose()
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelInitialized(IConventionModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelInitialized(IConventionModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model..ctor(ConventionSet conventions, ModelDependencies modelDependencies, ModelConfiguration modelConfiguration)
at Microsoft.EntityFrameworkCore.ModelBuilder..ctor(ConventionSet conventions, ModelDependencies modelDependencies, ModelConfiguration modelConfiguration)
at Microsoft.EntityFrameworkCore.ModelConfigurationBuilder.CreateModelBuilder(ModelDependencies modelDependencies)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, 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_ContextServices()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService(IInfrastructure`1 accessor, Type serviceType)
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.GetMigrations(String contextType, String connectionString, Boolean noConnect)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetMigrationsImpl(String contextType, String connectionString, Boolean noConnect)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetMigrations.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create a 'DbContext' of type ''. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
PM>
Include verbose output
Here's the output from the Contoso U sample that works:
PM> Get-Migration -Project CU.Infrastructure -StartupProject CU.EFDataApp -verbose
Using project 'src\CU.Infrastructure'.
Using startup project 'src\CU.EFDataApp'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\bin\Debug\net8.0\CU.EFDataApp.deps.json --additionalprobingpath C:\Users\usr_x\.nuget\packages --runtimeconfig D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\bin\Debug\net8.0\CU.EFDataApp.runtimeconfig.json C:\Users\usr_x\.nuget\packages\microsoft.entityframeworkcore.tools\8.0.0\tools\netcoreapp2.0\any\ef.dll migrations list --json --verbose --no-color --prefix-output --assembly D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\bin\Debug\net8.0\CU.Infrastructure.dll --project D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.Infrastructure\CU.Infrastructure.csproj --startup-assembly D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\bin\Debug\net8.0\CU.EFDataApp.dll --startup-project D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\CU.EFDataApp.csproj --project-dir D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.Infrastructure\ --language C# --configuration Debug --working-dir D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF --root-namespace CU.Infrastructure --nullable
Using assembly 'CU.Infrastructure'.
Using startup assembly 'CU.EFDataApp'.
Using application base 'D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp\bin\Debug\net8.0'.
Using working directory 'D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.EFDataApp'.
Using root namespace 'CU.Infrastructure'.
Using project directory 'D:\_dev\GitHub\xyzabc\EF\ContosoU_dn8_EF\src\CU.Infrastructure\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'SchoolContextFactory'.
Found DbContext 'SchoolDbContext'.
Finding application service provider in assembly 'CU.EFDataApp'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Finding DbContext classes in the project...
Using DbContext factory 'SchoolContextFactory'.
Using context 'SchoolDbContext'.
Finding design-time services referenced by assembly 'CU.EFDataApp'...
Finding design-time services referenced by assembly 'CU.Infrastructure'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'CU.EFDataApp'...
No design-time services were found.
Creating DbConnection.
Created DbConnection. (37ms).
Opening connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Opened connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Creating DbCommand for 'ExecuteNonQuery'.
Created DbCommand for 'ExecuteNonQuery' (2ms).
Initialized DbCommand for 'ExecuteNonQuery' (6ms).
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Closing connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Closed connection to database 'ContosoU_dn6_dev' on server 'wh2f' (3ms).
Creating DbCommand for 'ExecuteScalar'.
Created DbCommand for 'ExecuteScalar' (0ms).
Initialized DbCommand for 'ExecuteScalar' (0ms).
Opening connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Opened connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Closing connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Closed connection to database 'ContosoU_dn6_dev' on server 'wh2f' (0ms).
'SchoolDbContext' disposed.
Disposing connection to database 'ContosoU_dn6_dev' on server 'wh2f'.
Disposed connection to database '' on server '' (0ms).
id name safeName applied
-- ---- -------- -------
20220303023706_CU6_M01_ExistingSchemaBase_2022 CU6_M01_ExistingSchemaBase_2022 CU6_M01_ExistingSchemaBase_2022 False
20220303033131_CU6_M02_AddEnrollment CU6_M02_AddEnrollment CU6_M02_AddEnrollment False
20220303041924_CU6_M03_AddCourseInstructorLink CU6_M03_AddCourseInstructorLink CU6_M03_AddCourseInstructorLink False
PM>
Include provider and version information
EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.8.1