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

Can't generate a compiled model with keyless entity #26195

Closed
ZvonimirMatic opened this issue Sep 28, 2021 · 3 comments
Closed

Can't generate a compiled model with keyless entity #26195

ZvonimirMatic opened this issue Sep 28, 2021 · 3 comments

Comments

@ZvonimirMatic
Copy link

ZvonimirMatic commented Sep 28, 2021

I was unable to generate a compiled model (#1906) with the rc1 version of EF Core. I installed the rc1 version of .NET 6 and dotnet ef tool. I tried the same thing with SqlServer and SqlLite, both produce the same result. I was able to do this with the previous versions without a problem, and by definition of the keyless entity type, I believe it shouldn't be a problem.

To reproduce the bug:

  • Create a new project (dotnet new web --name EFCoreCompiledModelKeyless)
  • Install the rc1 version of Microsft.EntityFrameworkCore.SqlServer and Microsft.EntityFrameworkCore.Design in the project
  • Add Model.cs file with the content below
  • Replace Program.cs content with the content below
  • Run dotnet ef dbcontext optimize --context MyDbContext --output-dir Compiled --verbose

Model.cs

using Microsoft.EntityFrameworkCore;

public class MyKeylessEntity
{
    public int MyKeyfullEntityId { get; set; }
    public MyKeyfullEntity MyKeyfullEntity { get; set; }
}

public class MyKeyfullEntity
{
    public int Id { get; set; }
}

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyKeyfullEntity>()
            .HasKey(x => x.Id);

        modelBuilder.Entity<MyKeylessEntity>()
            .HasNoKey();

        modelBuilder.Entity<MyKeylessEntity>()
            .HasOne(x => x.MyKeyfullEntity)
            .WithMany()
            .HasForeignKey(x => x.MyKeyfullEntityId);
    }
}

Program.cs

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlServer("conn"));

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

The result is the following:

Using project 'C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj'.
Using startup project 'C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj'.
Writing 'C:\current\EFCoreCompiledModelKeyless\obj\EFCoreCompiledModelKeyless.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\zvoni\AppData\Local\Temp\tmp75A0.tmp /verbosity:quiet 
/nologo C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj
Writing 'C:\current\EFCoreCompiledModelKeyless\obj\EFCoreCompiledModelKeyless.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\zvoni\AppData\Local\Temp\tmp79E7.tmp /verbosity:quiet 
/nologo C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj
Build started...
dotnet build C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj /verbosity:quiet /nologo

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

Time Elapsed 00:00:02.97
Build succeeded.
dotnet exec --depsfile C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.deps.json --additionalprobingpath 
C:\Users\zvoni\.nuget\packages --additionalprobingpath C:\Microsoft\Xamarin\NuGet --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.runtimeconfig.json C:\Users\zvoni\.dotnet\tools\.store\dotnet-ef\6.0.0-rc.1.21452.10\dotnet-ef\6.0.0-rc.1.21452.10\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll dbcontext optimize --context MyDbContext --output-dir Compiled --assembly C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.dll --project C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj --startup-assembly C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.dll --startup-project C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj --project-dir C:\current\EFCoreCompiledModelKeyless\ --root-namespace EFCoreCompiledModelKeyless --language C# --framework net6.0 --working-dir C:\current\EFCoreCompiledModelKeyless --verbose
Using assembly 'EFCoreCompiledModelKeyless'.
Using startup assembly 'EFCoreCompiledModelKeyless'.
Using application base 'C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0'.
Using working directory 'C:\current\EFCoreCompiledModelKeyless'.
Using root namespace 'EFCoreCompiledModelKeyless'.
Using project directory 'C:\current\EFCoreCompiledModelKeyless\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'EFCoreCompiledModelKeyless'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'MyDbContext'.
Finding DbContext classes in the project...
Using context 'MyDbContext'.
System.InvalidOperationException: The navigation '' cannot be added because it targets the keyless entity type 'MyKeylessEntity'. Navigations can only target entity types with keys.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Navigation(Nullable`1 propertyIdentity, ConfigurationSource configurationSource, Boolean pointsToPrincipal)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.SetPrincipalToDependent(String name, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, EntityType principalEntityType, EntityType dependentEntityType, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(String navigationToPrincipalName, String navigationToDependentName, EntityType principalEntityType, EntityType dependentEntityType, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder.WithManyBuilder(MemberIdentity collection)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder.WithManyBuilder(String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder`2.WithMany(String navigationName)
   at MyDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\current\EFCoreCompiledModelKeyless\Model.cs:line 65
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
   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__7_3(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) 
PS C:\current\EFCoreCompiledModelKeyless> dotnet ef dbcontext optimize --context MyDbContext --output-dir Compiled --verbose
Using project 'C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj'.
Using startup project 'C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj'.
Writing 'C:\current\EFCoreCompiledModelKeyless\obj\EFCoreCompiledModelKeyless.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\zvoni\AppData\Local\Temp\tmpB885.tmp /verbosity:quiet 
/nologo C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj
Writing 'C:\current\EFCoreCompiledModelKeyless\obj\EFCoreCompiledModelKeyless.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\zvoni\AppData\Local\Temp\tmpBDE5.tmp /verbosity:quiet 
/nologo C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj
Build started...
dotnet build C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj /verbosity:quiet /nologo

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

Time Elapsed 00:00:03.05
Build succeeded.
dotnet exec --depsfile C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.deps.json --additionalprobingpath 
C:\Users\zvoni\.nuget\packages --additionalprobingpath C:\Microsoft\Xamarin\NuGet --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.runtimeconfig.json C:\Users\zvoni\.dotnet\tools\.store\dotnet-ef\6.0.0-rc.1.21452.10\dotnet-ef\6.0.0-rc.1.21452.10\tools\netcoreapp3.1\any\tools\netcoreapp2.0\any\ef.dll dbcontext optimize --context MyDbContext --output-dir Compiled --assembly C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.dll --project C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj --startup-assembly C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0\EFCoreCompiledModelKeyless.dll --startup-project C:\current\EFCoreCompiledModelKeyless\EFCoreCompiledModelKeyless.csproj --project-dir C:\current\EFCoreCompiledModelKeyless\ --root-namespace EFCoreCompiledModelKeyless --language C# --framework net6.0 --working-dir C:\current\EFCoreCompiledModelKeyless --verbose
Using assembly 'EFCoreCompiledModelKeyless'.
Using startup assembly 'EFCoreCompiledModelKeyless'.
Using application base 'C:\current\EFCoreCompiledModelKeyless\bin\Debug\net6.0'.
Using working directory 'C:\current\EFCoreCompiledModelKeyless'.
Using root namespace 'EFCoreCompiledModelKeyless'.
Using project directory 'C:\current\EFCoreCompiledModelKeyless\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'EFCoreCompiledModelKeyless'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'MyDbContext'.
Finding DbContext classes in the project...
Using context 'MyDbContext'.
System.InvalidOperationException: The navigation '' cannot be added because it targets the keyless entity type 'MyKeylessEntity'. Navigations can only target entity types with keys.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.Navigation(Nullable`1 propertyIdentity, ConfigurationSource configurationSource, Boolean pointsToPrincipal)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.SetPrincipalToDependent(String name, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, EntityType principalEntityType, EntityType dependentEntityType, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.HasNavigations(String navigationToPrincipalName, String navigationToDependentName, EntityType principalEntityType, EntityType dependentEntityType, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder.WithManyBuilder(MemberIdentity collection)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder.WithManyBuilder(String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceNavigationBuilder`2.WithMany(String navigationName)
   at MyDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\current\EFCoreCompiledModelKeyless\Model.cs:line 65
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
   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__7_3(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(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_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[TService](IInfrastructure`1 accessor)      
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.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.DbContextOperations.Optimize(String outputDir, String modelNamespace, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContextImpl(String outputDir, String modelNamespace, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The navigation '' cannot be added because it targets the keyless entity type 'MyKeylessEntity'. Navigations can only target entity types with keys.

EF Core version: 6.0 RC1
Database provider: Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.SqlLite
Target framework: .NET 6.0 RC1
Operating system: Windows 10
IDE: VS Code

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 28, 2021

The navigation '' cannot be added because it targets the keyless entity type 'MyKeylessEntity'. Navigations can only target entity types with keys.

@ZvonimirMatic
Copy link
Author

@ErikEJ but MyKeyfullEntity doesn't have a navigation that targets MyKeylessEntity. The navigation is defined on the keyless entity, which shouldn't be a problem. By definition, these are keyless entity constraints:

  • They may never act as the principal end of a relationship.
  • They may not have navigations to owned entities
  • They can only contain reference navigation properties pointing to regular entities.
  • Entities cannot contain navigation properties to keyless entity types.

MyKeylessEntity is the dependant end of the relationship, doesn't have a navigation to an owned entity, contains a reference navigation property only to regular entity (MyKeyfullEntity) and no other entity contains a navigation property to it.

To me it seems like there might be a bug in part of the code that checks for navigation properties pointing to keyless entities - if the navigation property's name is empty, it clearly isn't defined.

@smitpatel
Copy link
Contributor

Duplicate of #26073

@smitpatel smitpatel marked this as a duplicate of #26073 Sep 28, 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

4 participants