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

Improve documentation and exception message for .Navigation() #34780

Open
voroninp opened this issue Sep 27, 2024 · 7 comments
Open

Improve documentation and exception message for .Navigation() #34780

voroninp opened this issue Sep 27, 2024 · 7 comments
Labels
area-model-building customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Milestone

Comments

@voroninp
Copy link

File a bug

I am getting Navigation was not found. exception when creating a migration.

Include your code

See this repository

Here are the mapped classes:

public sealed class Owner
{
    public int Id { get; private set; }

    private readonly List<Owned> _ownedItems = new List<Owned>();

    public IReadOnlyList<Owned> Items { get; }

    public void AddItem(Owned item)
    {
        _ownedItems.Add(item);
    }

    public Owner()
    {
        Items = _ownedItems.AsReadOnly();
    }
}

public sealed class Owned
{
    public int Id { get; private set; }

    public int OwnerId { get; private set; }

    private readonly List<Owned2> _ownedItems = new List<Owned2>();

    public IReadOnlyList<Owned2> Items { get; }

    public Owned()
    {
        Items = _ownedItems.AsReadOnly();
    }

    public void AddItem(Owned2 item)
    {
        _ownedItems.Add(item);
    }
}

public sealed class Owned2
{
    public int Id { get; private set; }

    public int OwnerId { get; private set; }
}

And here's the mapping part:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Owner>(b =>
    {
        b.Navigation(e => e.Items).HasField("_ownedItems");
        b.OwnsMany(e => e.Items, b =>
        {
            b.WithOwner().HasForeignKey(e => e.OwnerId);
            b.HasKey(e => e.Id);
            // If you comment this out, migrations work.
            b.Navigation(e => e.Items).HasField("_ownedItems");
            b.OwnsMany(e => e.Items, b =>
            {
                b.WithOwner().HasForeignKey(e => e.OwnerId);
                b.HasKey(e => e.Id);
            });
        });
    });
}

While first b.Navigation(e => e.Items).HasField("_ownedItems"); works fine. The one for nested collection causes migration to fail.

Include verbose output

Using project 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj'.
Using startup project 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj'.
Writing 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\obj\EfBackingFields.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\pavel\AppData\Local\Temp\tmpoi5ukq.tmp /verbosity:quiet /nologo C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj
Writing 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\obj\EfBackingFields.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\pavel\AppData\Local\Temp\tmph2kupa.tmp /verbosity:quiet /nologo C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj
Build started...
dotnet build C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj /verbosity:quiet /nologo /p:PublishAot=false

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

Time Elapsed 00:00:01.73

Workload updates are available. Run `dotnet workload list` for more information.
Build succeeded.
dotnet exec --depsfile C:\Users\pavel\source\repos\voroninp\EfBackingFields\bin\Debug\net8.0\EfBackingFields.deps.json --additionalprobingpath C:\Users\pavel\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\DevExpress 21.1\Components\Offline Packages" --additionalprobingpath "C:\Program Files\DevExpress 22.2\Components\Offline Packages" --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\Users\pavel\source\repos\voroninp\EfBackingFields\bin\Debug\net8.0\EfBackingFields.runtimeconfig.json C:\Users\pavel\.dotnet\tools\.store\dotnet-ef\8.0.8\dotnet-ef\8.0.8\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations add Migration --assembly C:\Users\pavel\source\repos\voroninp\EfBackingFields\bin\Debug\net8.0\EfBackingFields.dll --project C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj --startup-assembly C:\Users\pavel\source\repos\voroninp\EfBackingFields\bin\Debug\net8.0\EfBackingFields.dll --startup-project C:\Users\pavel\source\repos\voroninp\EfBackingFields\EfBackingFields.csproj --project-dir C:\Users\pavel\source\repos\voroninp\EfBackingFields\ --root-namespace EfBackingFields --language C# --framework net8.0 --nullable --working-dir C:\Users\pavel\source\repos\voroninp\EfBackingFields --verbose
Using assembly 'EfBackingFields'.
Using startup assembly 'EfBackingFields'.
Using application base 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\bin\Debug\net8.0'.
Using working directory 'C:\Users\pavel\source\repos\voroninp\EfBackingFields'.
Using root namespace 'EfBackingFields'.
Using project directory 'C:\Users\pavel\source\repos\voroninp\EfBackingFields\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'EfBackingFields'...
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 'Ctx'.
Using context 'Ctx'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type ''. The exception 'Navigation 'Owned.Items' was not found. Please add the navigation to the entity type before configuring it.' 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.InvalidOperationException: Navigation 'Owned.Items' was not found. Please add the navigation to the entity type before configuring it.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Navigation(String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Navigation(MemberInfo memberInfo)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder`2.Navigation[TNavigation](Expression`1 navigationExpression)
   at Ctx.<>c.<OnModelCreating>b__4_3(OwnedNavigationBuilder`2 b) in C:\Users\pavel\source\repos\voroninp\EfBackingFields\Program.cs:line 43
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.OwnsMany[TRelatedEntity](Expression`1 navigationExpression, Action`1 buildAction)
   at Ctx.<>c.<OnModelCreating>b__4_0(EntityTypeBuilder`1 b) in C:\Users\pavel\source\repos\voroninp\EfBackingFields\Program.cs:line 38
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action`1 buildAction)
   at Ctx.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\pavel\source\repos\voroninp\EfBackingFields\Program.cs:line 35
   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__8_4(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, 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.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.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   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)
Unable to create a 'DbContext' of type ''. The exception 'Navigation 'Owned.Items' was not found. Please add the navigation to the entity type before configuring it.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Include provider and version information

EF Core version: 8.0.8
Database provider: does not matter. Fails both for SQL Server and Postgres.
Target framework: (e.g. .NET 8.0)
Operating system: Windows 11 Pro 23H2
IDE: Visual Studio 2022 17.12 Preview 2.1

@maumar
Copy link
Contributor

maumar commented Oct 2, 2024

it still repros on current main, however if one swaps the order of OwnsMany and Navigation calls in the nested case (i.e. OwnsMany first then Navigation) things work just fine:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Owner>(b =>
    {
        b.Navigation(e => e.Items).HasField("_ownedItems");
        b.OwnsMany(e => e.Items, b =>
        {
            b.WithOwner().HasForeignKey(e => e.OwnerId);
            b.HasKey(e => e.Id);
            // If you comment this out, migrations work.
            b.Navigation(e => e.Items).HasField("_ownedItems");
            b.OwnsMany(e => e.Items, b =>
            {
                b.WithOwner().HasForeignKey(e => e.OwnerId);
                b.HasKey(e => e.Id);
            });
        });
    });
}

@AndriySvyryd
Copy link
Member

This is expected, the relationship needs to be configured (with OwnsMany, HasOne, etc.) before Navigation can be called if it's not discovered by convention.

@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2024
@AndriySvyryd AndriySvyryd removed their assignment Oct 3, 2024
@AndriySvyryd AndriySvyryd added the closed-no-further-action The issue is closed and no further action is planned. label Oct 3, 2024
@voroninp
Copy link
Author

voroninp commented Oct 4, 2024

Ok, thanks, @AndriySvyryd.

So add navigation from the error message means calling Has|OwnsOne|Many. Not that obvious TBH, because it's all about configuring mappings. Maybe you could slightly change the wording of the error message?

And this xmldoc for Navigation method is likely missing a remarks section ;-)

/// <summary>
///     Returns an object that can be used to configure an existing navigation property
///     from the owned type to its owner. It is an error for the navigation property
///     not to exist.
/// </summary>
/// <typeparam name="TNavigation">The target entity type.</typeparam>
/// <param name="navigationExpression">
///     A lambda expression representing the navigation property to be configured (
///     <c>blog => blog.Posts</c>).
/// </param>
/// <returns>An object that can be used to configure the navigation property.</returns>

Is there an analyzer for this, btw?

@AndriySvyryd AndriySvyryd reopened this Oct 4, 2024
@AndriySvyryd AndriySvyryd changed the title Cannot specify backing field for nested owned entities. Improve documentation and exception message for .Navigation() Oct 4, 2024
@AndriySvyryd AndriySvyryd added good first issue This issue should be relatively straightforward to fix. area-model-building and removed closed-no-further-action The issue is closed and no further action is planned. labels Oct 4, 2024
@AndriySvyryd AndriySvyryd added this to the Backlog milestone Oct 4, 2024
@voroninp
Copy link
Author

voroninp commented Oct 9, 2024

@AndriySvyryd , by the way, I see that order:
Navigation() and HasMany() does not cause an issue.
Looks like it matters only for owned entities.

@AndriySvyryd
Copy link
Member

@AndriySvyryd , by the way, I see that order:
Navigation() and HasMany() does not cause an issue.
Looks like it matters only for owned entities.

It does if the navigation is not discovered by convention. E.g. it has [Ignore] on it.

@voroninp
Copy link
Author

voroninp commented Oct 9, 2024

@AndriySvyryd Will public property of any type implementing IEnumerable<TEntity> be treated as navigation?

@AndriySvyryd
Copy link
Member

@AndriySvyryd Will public property of any type implementing IEnumerable be treated as navigation?

Yes, in most cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants