From 8f4a0d6c87903a141c9bb0a85854f60373f37641 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Tue, 10 Sep 2024 15:49:50 -0700 Subject: [PATCH] [release/9.0] Remove referencing indexes when reconfiguring a property as a navigation (#34640) Fixes #29997 --- .../Internal/InternalTypeBaseBuilder.cs | 2 +- .../CosmosModelBuilderGenericTest.cs | 24 +++++++------------ .../ModelBuilderTest.OwnedTypes.cs | 6 +++++ .../ModelBuilderTest.TestModel.cs | 8 +++++++ 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs b/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs index 2183bbce984..0e889e243f4 100644 --- a/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs @@ -774,7 +774,7 @@ public virtual void RemoveMembersInHierarchy(string propertyName, ConfigurationS { if (conflictingProperty.GetConfigurationSource() != ConfigurationSource.Explicit) { - conflictingProperty.DeclaringType.RemoveProperty(conflictingProperty); + conflictingProperty.DeclaringType.Builder.RemoveProperty(conflictingProperty, configurationSource); } } diff --git a/test/EFCore.Cosmos.FunctionalTests/ModelBuilding/CosmosModelBuilderGenericTest.cs b/test/EFCore.Cosmos.FunctionalTests/ModelBuilding/CosmosModelBuilderGenericTest.cs index 82b70d4313b..0865ab00f6d 100644 --- a/test/EFCore.Cosmos.FunctionalTests/ModelBuilding/CosmosModelBuilderGenericTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/ModelBuilding/CosmosModelBuilderGenericTest.cs @@ -1251,28 +1251,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.Specification.Tests/ModelBuilding/ModelBuilderTest.OwnedTypes.cs b/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.OwnedTypes.cs index c1a251a6471..e8371d55489 100644 --- a/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.OwnedTypes.cs +++ b/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.OwnedTypes.cs @@ -599,6 +599,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.Specification.Tests/ModelBuilding/ModelBuilderTest.TestModel.cs b/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.TestModel.cs index 7bfb5484941..1af3de98e34 100644 --- a/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.TestModel.cs +++ b/test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.TestModel.cs @@ -1112,16 +1112,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; } } @@ -1189,6 +1196,7 @@ protected class OneToManyOwnedWithField public OneToManyOwnerWithField? OneToManyOwner { get; set; } } + [Index(nameof(OwnedDependent))] protected class OneToOneOwnerWithField { public int Id;