Open voroninp opened 1 month ago
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);
});
});
});
}
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.
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 , by the way, I see that order:
Navigation()
and HasMany()
does not cause an issue.
Looks like it matters only for owned entities.
@AndriySvyryd , by the way, I see that order:
Navigation()
andHasMany()
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.
@AndriySvyryd Will public property of any type implementing IEnumerable<TEntity>
be treated as navigation?
@AndriySvyryd Will public property of any type implementing IEnumerable
be treated as navigation?
Yes, in most cases
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:
And here's the mapping part:
While first
b.Navigation(e => e.Items).HasField("_ownedItems");
works fine. The one for nested collection causes migration to fail.Include verbose output
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