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

System.NullReferenceException thrown for many-to-many without navigation properties on one or both sides of the relationship #25383

Closed
JakeYallop opened this issue Aug 1, 2021 · 5 comments

Comments

@JakeYallop
Copy link

When creating a many-to-many mapping in EF Core 5, we did not need to specify any navigation properties on the entities making up the relationship. The below code works in EF Core 5.0.8:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Role>()
        .HasMany(x => x.Permissions)
        //We don't specify a navigation property here - this works fine in EF Core 5.0.8
        .WithMany("Roles")
        .UsingEntity<RolePermission>(
            x => x.HasOne(x => x.Permission).WithMany(),
            x => x.HasOne(x => x.Role).WithMany());
}

This no longer works in EF Core 6.0 Preview 6. Trying to script a migration throws a NullReferenceException, and for existing code (I was migrating a NET 5.0 project to NET 6.0), this throws a NRE at runtime (with a mostly identical stack trace),

Reproduceable code example:
https://github.com/JakeYal/EFCore6ManyToManyRepro
To reproduce, just run dotnet ef migrations add initial

dotnet-ef version:

> dotnet tool list --global
Package Id                    Version                      Commands
-------------------------------------------------------------------------
dotnet-ef                     6.0.0-preview.6.21352.1      dotnet-ef
Migration stack trace and verbose output
> dotnet ef migrations add initial --verbose

Using project 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\Project.csproj'.
Using startup project 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\Project.csproj'.
Writing 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\obj\Project.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\JakeYal\AppData\Local\Temp\tmpA681.tmp /verbosity:quiet /nologo C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\Project.csproj
Writing 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\obj\Project.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\JakeYal\AppData\Local\Temp\tmpA819.tmp /verbosity:quiet /nologo C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\Project.csproj
Build started...
dotnet build C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\Project.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.76
Build succeeded.
dotnet exec --depsfile C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\bin\Debug\net6.0\Project.deps.json --additionalprobingpath C:\Users\JakeYal\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft\Xamarin\NuGet" --runtimeconfig C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\bin\Debug\net6.0\Project.runtimeconfig.json C:\Users\JakeYal\.dotnet\tools\.store\dotnet-ef\6.0.0-preview.6.21352.1\dotnet-ef\6.0.0-preview.6.21352.1\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll migrations add initial --assembly C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\bin\Debug\net6.0\Project.dll --startup-assembly C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\bin\Debug\net6.0\Project.dll --project-dir C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\ --root-namespace Project --language C# --working-dir C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project --verbose
Using assembly 'Project'.
Using startup assembly 'Project'.
Using application base 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\bin\Debug\net6.0'.
Using working directory 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project'.
Using root namespace 'Project'.
Using project directory 'C:\Users\JakeYal\source\repos\EFCore6.0Repro\src\Project\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Project'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MyDbContext'.
Using context 'MyDbContext'.
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Reflection.EntityFrameworkMemberInfoExtensions.GetMemberType(MemberInfo memberInfo) in Microsoft.EntityFrameworkCore.dll:token 0x600004c+0x16
   at Microsoft.EntityFrameworkCore.Metadata.Internal.SkipNavigation.get_ClrType() in Microsoft.EntityFrameworkCore.dll:token 0x600146a+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.Create(ISkipNavigation navigation, RuntimeEntityType runtimeEntityType) in Microsoft.EntityFrameworkCore.dll:token 0x6001655+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.Create(IModel model) in Microsoft.EntityFrameworkCore.dll:token 0x6001642+0x3cc
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.ProcessModelFinalized(IModel model) in Microsoft.EntityFrameworkCore.dll:token 0x6001641+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.OnModelFinalized() in Microsoft.EntityFrameworkCore.dll:token 0x60012cb+0x10
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.<>c.<Initialize>b__4_0(ValueTuple`4 args) in Microsoft.EntityFrameworkCore.dll:token 0x6002cf0+0x6b
   at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.<>c__30`2.<GetOrAddRuntimeAnnotationValue>b__30_0(String n, ValueTuple`3 t) in Microsoft.EntityFrameworkCore.dll:token 0x6002cca+0x0
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument) in System.Collections.Concurrent.dll:token 0x60000d4+0x4f
   at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.GetOrAddRuntimeAnnotationValue[TValue,TArg](String name, Func`2 valueFactory, TArg factoryArgument) in Microsoft.EntityFrameworkCore.dll:token 0x60019f2+0x2e
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger) in Microsoft.EntityFrameworkCore.dll:token 0x6001b26+0x56
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime) in Microsoft.EntityFrameworkCore.dll:token 0x6001b3b+0x5b
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime) in Microsoft.EntityFrameworkCore.dll:token 0x6001c36+0xcd
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() in Microsoft.EntityFrameworkCore.dll:token 0x6001c38+0xa
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p) in Microsoft.EntityFrameworkCore.dll:token 0x6002cda+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000096+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x33
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x39
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x39
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x39
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x39
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x39
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a9+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x66
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0xe
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x57
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008c+0x12
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600016c+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000067+0x33
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000110+0xd
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) in Microsoft.Extensions.DependencyInjection.Abstractions.dll:token 0x6000095+0x34
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) in Microsoft.Extensions.DependencyInjection.Abstractions.dll:token 0x6000096+0xe
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() in Microsoft.EntityFrameworkCore.dll:token 0x6000088+0x10
   at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() in Microsoft.EntityFrameworkCore.dll:token 0x6000087+0xb0
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() in Microsoft.EntityFrameworkCore.dll:token 0x6000086+0x0
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() in Microsoft.EntityFrameworkCore.dll:token 0x60000c6+0x0
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor) in Microsoft.EntityFrameworkCore.dll:token 0x6001bce+0x0
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor) in Microsoft.EntityFrameworkCore.dll:token 0x60019d0+0x0
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory) in Microsoft.EntityFrameworkCore.Design.dll:token 0x60003cb+0x22
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) in Microsoft.EntityFrameworkCore.Design.dll:token 0x60003ca+0x0
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace) in Microsoft.EntityFrameworkCore.Design.dll:token 0x60003f4+0x2a
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace) in Microsoft.EntityFrameworkCore.Design.dll:token 0x6000367+0xc
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() in Microsoft.EntityFrameworkCore.Design.dll:token 0x6000630+0x0
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0() in Microsoft.EntityFrameworkCore.Design.dll:token 0x6000644+0x0
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) in Microsoft.EntityFrameworkCore.Design.dll:token 0x60005bb+0xc
Object reference not set to an instance of an object.
Runtime NullReferenceException stack trace
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Reflection.EntityFrameworkMemberInfoExtensions.GetMemberType(MemberInfo memberInfo) in Microsoft.EntityFrameworkCore.dll:token 0x600004c+0x16
   at Microsoft.EntityFrameworkCore.Metadata.Internal.SkipNavigation.get_ClrType() in Microsoft.EntityFrameworkCore.dll:token 0x600146a+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.Create(ISkipNavigation navigation, RuntimeEntityType runtimeEntityType) in Microsoft.EntityFrameworkCore.dll:token 0x6001655+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.Create(IModel model) in Microsoft.EntityFrameworkCore.dll:token 0x6001642+0x3cc
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.RuntimeModelConvention.ProcessModelFinalized(IModel model) in Microsoft.EntityFrameworkCore.dll:token 0x6001641+0x0
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.OnModelFinalized() in Microsoft.EntityFrameworkCore.dll:token 0x60012cb+0x10
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.<>c.<Initialize>b__4_0(ValueTuple`4 args) in Microsoft.EntityFrameworkCore.dll:token 0x6002cf0+0x6b
   at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.<>c__30`2.<GetOrAddRuntimeAnnotationValue>b__30_0(String n, ValueTuple`3 t) in Microsoft.EntityFrameworkCore.dll:token 0x6002cca+0x0
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument) in System.Collections.Concurrent.dll:token 0x60000d4+0x4f
   at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.GetOrAddRuntimeAnnotationValue[TValue,TArg](String name, Func`2 valueFactory, TArg factoryArgument) in Microsoft.EntityFrameworkCore.dll:token 0x60019f2+0x2e
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger) in Microsoft.EntityFrameworkCore.dll:token 0x6001b26+0x56
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime) in Microsoft.EntityFrameworkCore.dll:token 0x6001b3b+0x5b
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime) in Microsoft.EntityFrameworkCore.dll:token 0x6001c36+0xcd
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() in Microsoft.EntityFrameworkCore.dll:token 0x6001c38+0xa
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p) in Microsoft.EntityFrameworkCore.dll:token 0x6002cda+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000096+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x9b
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0x1f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x77
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x9b
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0x1f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x77
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008e+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x9b
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0x1f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x77
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008c+0x12
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600016c+0x46
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000067+0x3b
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) in Microsoft.Extensions.DependencyInjection.Abstractions.dll:token 0x6000095+0x4f
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) in Microsoft.Extensions.DependencyInjection.Abstractions.dll:token 0x6000096+0xd
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_7(IServiceProvider p) in Microsoft.EntityFrameworkCore.dll:token 0x6002cde+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000096+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x9b
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0x1f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x77
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000095+0x0
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000091+0x9b
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000090+0x1f
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) in Microsoft.Extensions.DependencyInjection.dll:token 0x60000a8+0x77
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600008c+0x12
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope) in Microsoft.Extensions.DependencyInjection.dll:token 0x600016c+0x46
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000067+0x3b
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) in Microsoft.Extensions.DependencyInjection.Abstractions.dll:token 0x6000094+0xe
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IResettableService.ResetStateAsync(CancellationToken cancellationToken) in Microsoft.EntityFrameworkCore.dll:token 0x600009d+0x11
   at Microsoft.EntityFrameworkCore.Internal.DbContextPool`1.ReturnAsync(IDbContextPoolable context, CancellationToken cancellationToken) in Microsoft.EntityFrameworkCore.dll:token 0x6001c28+0x89
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.<DisposeAsync>g__Await|21_0(Int32 i, ValueTask vt, List`1 toDispose) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000117+0x6e
   at Microsoft.AspNetCore.Http.Features.RequestServicesFeature.<DisposeAsync>g__Awaited|9_0(RequestServicesFeature servicesFeature, ValueTask vt) in Microsoft.AspNetCore.Http.dll:token 0x6000232+0x5b
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<FireOnCompleted>g__ProcessEvents|227_0(HttpProtocol protocol, Stack`1 events) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x6000b24+0x81

Provider and version information

EF Core version: 6.0.0-preview.6.21352.1
Database provider: Tested with Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 17.0.0 Preview 2.1

@ajcvickers
Copy link
Member

Note for triage: Model created with EF Core 5.0.8 below. Note the skip-navigation for "Roles" that is not a shadow nav but also not mapped to any CLR field or property.

Model: 
  EntityType: Permission
    Properties: 
      Id (Guid) Required PK AfterSave:Throw ValueGenerated.OnAdd
    Skip navigations: 
      Roles (no field, ) CollectionRole Inverse: Permissions
    Keys: 
      Id PK
  EntityType: Role
    Properties: 
      Id (Guid) Required PK AfterSave:Throw ValueGenerated.OnAdd
    Skip navigations: 
      Permissions (ICollection<Permission>) CollectionPermission Inverse: Roles
    Keys: 
      Id PK
  EntityType: RolePermission
    Properties: 
      PermissionId (Guid) Required PK FK AfterSave:Throw
      RoleId (Guid) Required PK FK Index AfterSave:Throw
    Navigations: 
      Permission (Permission) ToPrincipal Permission
      Role (Role) ToPrincipal Role
    Keys: 
      PermissionId, RoleId PK
    Foreign keys: 
      RolePermission {'PermissionId'} -> Permission {'Id'} ToPrincipal: Permission Cascade
      RolePermission {'RoleId'} -> Role {'Id'} ToPrincipal: Role Cascade
    Indexes: 
      RoleId 

@ajcvickers
Copy link
Member

Notes from triage: this was not intended to work in 5.0, since there is no "Roles" property or field. This requires unidirectional skip navigations, which is tracked by #3864. However, we were not correctly catching this in model building, and it unfortunately worked accidentally for some scenarios.

@JakeYal The workaround until #3864 is implemented is to add a private navigation property for the inverse. For example:

public class Permission
{
    public Guid Id { get; set; }
    private ICollection<Role> Roles { get; set; } = null!;
}

This allows EF to create the metadata correctly and should therefore work in all scenarios, rather than only where the incorrect metadata does not cause other errors.

@AndriySvyryd I think you indicated in triage where this changed in 6.0, but I haven't been able to find it. Also, for this invalid case we should throw a better exception, potentially pointing to the workaround for people who have been using this pattern in 5.0.

@AndriySvyryd
Copy link
Member

I think we didn't add a better exception because we planned to add support for this in 6.0

@ajcvickers
Copy link
Member

Note from triage: Putting this in 6.0 to throw with a better message.

@AndriySvyryd
Copy link
Member

Duplicate of #23362

@AndriySvyryd AndriySvyryd marked this as a duplicate of #23362 Aug 31, 2021
@AndriySvyryd AndriySvyryd removed this from the 6.0.0 milestone Aug 31, 2021
@AndriySvyryd AndriySvyryd removed their assignment Aug 31, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants