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

Stop discovering relationships if the target entity type becomes shared #35453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1299,17 +1299,24 @@ public virtual void ProcessNavigationAdded(
if (ambiguityRemoved)
{
DiscoverRelationships(entityType.Builder, context);

if (!navigationBuilder.Metadata.IsInModel)
{
context.StopProcessing();
return;
}
}

if (targetAmbiguityRemoved)
{
DiscoverRelationships(targetEntityType.Builder, context);
}
}

if (!navigationBuilder.Metadata.IsInModel)
{
context.StopProcessing();
if (!navigationBuilder.Metadata.IsInModel)
{
context.StopProcessing();
return;
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/EFCore/Metadata/Internal/ForeignKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public virtual InternalForeignKeyBuilder Builder
/// </summary>
public virtual bool IsInModel
=> _builder is not null
&& DeclaringEntityType.IsInModel;
&& DeclaringEntityType.IsInModel
&& PrincipalEntityType.IsInModel;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1893,15 +1893,26 @@ public virtual void Shared_type_entity_types_with_FK_to_another_entity_works()
}

[ConditionalFact]
public virtual void Inheritance_where_base_has_multiple_owned_types_works()
public virtual void Can_have_multiple_owned_types_on_base()
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<BaseOwner>();
modelBuilder.Entity<BaseOwner>().OwnsOne(o => o.OwnedWithRef1);
modelBuilder.Entity<DerivedOwner>();

var model = modelBuilder.FinalizeModel();

Assert.Equal(4, model.GetEntityTypes().Count());
Assert.Equal(6, model.GetEntityTypes().Count());
var owner = model.FindEntityType(typeof(BaseOwner));

var ownership1 = owner.FindNavigation(nameof(BaseOwner.Owned1)).ForeignKey;
var owned1 = ownership1.DeclaringEntityType;
Assert.True(ownership1.IsOwnership);
Assert.Same(owner, ownership1.PrincipalEntityType);

var ownership3 = owner.FindNavigation(nameof(BaseOwner.OwnedWithRef1)).ForeignKey;
var owned3 = ownership3.DeclaringEntityType;
Assert.True(ownership3.IsOwnership);
Assert.Same(owner, ownership3.DependentToPrincipal.TargetEntityType);
}

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,22 +852,26 @@ protected class PrincipalShadowFk
protected class BaseOwner
{
public int Id { get; set; }
public required OwnedTypeInheritance1 Owned1 { get; set; }
public required OwnedTypeInheritance2 Owned2 { get; set; }
public OwnedTypeInheritance Owned1 { get; set; } = null!;
public OwnedTypeInheritance Owned2 { get; set; } = null!;
public OwnedTypeInheritanceWithReference OwnedWithRef1 { get; set; } = null!;
public OwnedTypeInheritanceWithReference OwnedWithRef2 { get; set; } = null!;
}

protected class DerivedOwner : BaseOwner;

[Owned]
protected class OwnedTypeInheritance1
protected class OwnedTypeInheritance
{
public string? Value { get; set; }
}

[Owned]
protected class OwnedTypeInheritance2
protected class OwnedTypeInheritanceWithReference
{
public string? Value { get; set; }
public int OwnerId { get; set; }
public BaseOwner? Owner { get; set; }
}

protected interface IReplaceable
Expand Down