diff --git a/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs b/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs index 24333f89885..0bc79b42feb 100644 --- a/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs @@ -18,6 +18,9 @@ public abstract class InternalTypeBaseBuilder : AnnotatableBuilder /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in @@ -754,7 +757,14 @@ public virtual void RemoveMembersInHierarchy(string propertyName, ConfigurationS { if (conflictingProperty.GetConfigurationSource() != ConfigurationSource.Explicit) { - conflictingProperty.DeclaringType.RemoveProperty(conflictingProperty); + if (UseOldBehavior29997) + { + conflictingProperty.DeclaringType.RemoveProperty(conflictingProperty); + } + else + { + conflictingProperty.DeclaringType.Builder.RemoveProperty(conflictingProperty, configurationSource); + } } } diff --git a/test/EFCore.Cosmos.Tests/ModelBuilding/CosmosModelBuilderGenericTest.cs b/test/EFCore.Cosmos.Tests/ModelBuilding/CosmosModelBuilderGenericTest.cs index 4a1fd8ecb06..a1236b475f0 100644 --- a/test/EFCore.Cosmos.Tests/ModelBuilding/CosmosModelBuilderGenericTest.cs +++ b/test/EFCore.Cosmos.Tests/ModelBuilding/CosmosModelBuilderGenericTest.cs @@ -1023,28 +1023,20 @@ public virtual void Reference_type_is_discovered_as_owned() { var modelBuilder = CreateModelBuilder(); - modelBuilder.Entity( - e => - { - e.Property(p => p.Id); - e.Property(p => p.AlternateKey); - e.Property(p => p.Description); - e.HasKey(p => p.Id); - }); + modelBuilder.Entity(); var model = modelBuilder.FinalizeModel(); - var owner = model.FindEntityType(typeof(OneToOneOwnerWithField)); - Assert.Equal(typeof(OneToOneOwnerWithField).FullName, owner.Name); - var ownership = owner.FindNavigation(nameof(OneToOneOwnerWithField.OwnedDependent)).ForeignKey; + var owner = model.FindEntityType(typeof(OwnerOfOwnees))!; + var ownership = owner.FindNavigation(nameof(OwnerOfOwnees.Ownee1))!.ForeignKey; Assert.True(ownership.IsOwnership); - Assert.Equal(nameof(OneToOneOwnerWithField.OwnedDependent), ownership.PrincipalToDependent.Name); - Assert.Equal(nameof(OneToOneOwnedWithField.OneToOneOwner), ownership.DependentToPrincipal.Name); - Assert.Equal(nameof(OneToOneOwnerWithField.Id), ownership.PrincipalKey.Properties.Single().Name); + Assert.Equal(nameof(OwnerOfOwnees.Ownee1), ownership.PrincipalToDependent!.Name); + Assert.Equal(nameof(Ownee1.Owner), ownership.DependentToPrincipal!.Name); + Assert.Equal(nameof(OwnerOfOwnees.Id), ownership.PrincipalKey.Properties.Single().Name); var owned = ownership.DeclaringEntityType; Assert.Single(owned.GetForeignKeys()); - Assert.NotNull(model.FindEntityType(typeof(OneToOneOwnedWithField))); - Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(OneToOneOwnedWithField))); + Assert.NotNull(model.FindEntityType(typeof(Ownee1))); + Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(Ownee1))); } protected override TestModelBuilder CreateModelBuilder(Action configure = null) diff --git a/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs b/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs index fa421eb73b7..c30d4d34bde 100644 --- a/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs +++ b/test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs @@ -559,6 +559,12 @@ public virtual void Can_call_Owner_fluent_api_after_calling_Entity() modelBuilder.Owned(); modelBuilder.Owned(); modelBuilder.Owned(); + + var model = modelBuilder.FinalizeModel(); + + var owner = model.FindEntityType(typeof(OwnerOfOwnees))!; + var ownership = owner.FindNavigation(nameof(OwnerOfOwnees.Ownee1))!.ForeignKey; + Assert.True(ownership.IsOwnership); } [Flags] diff --git a/test/EFCore.Tests/ModelBuilding/TestModel.cs b/test/EFCore.Tests/ModelBuilding/TestModel.cs index cc1a81e459e..39d6e45d067 100644 --- a/test/EFCore.Tests/ModelBuilding/TestModel.cs +++ b/test/EFCore.Tests/ModelBuilding/TestModel.cs @@ -1144,16 +1144,23 @@ protected class OwnerOfOwnees protected class Ownee1 { + public string Data { get; private set; } = ""; + + public OwnerOfOwnees Owner { get; private set; } = null!; public Ownee3? NewOwnee3 { get; private set; } } protected class Ownee2 { + public Guid Data { get; private set; } + public Ownee3? Ownee3 { get; private set; } } protected class Ownee3 { + public DateTime Data { get; private set; } + public string? Name { get; private set; } } @@ -1221,6 +1228,7 @@ protected class OneToManyOwnedWithField public OneToManyOwnerWithField? OneToManyOwner { get; set; } } + [Index(nameof(OwnedDependent))] protected class OneToOneOwnerWithField { public int Id;