Skip to content

Commit 43718a7

Browse files
authored
[release/8.0] Remove referencing indexes when reconfiguring a property as a navigation (#34651)
Fixes #29997
1 parent 590bc43 commit 43718a7

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public abstract class InternalTypeBaseBuilder : AnnotatableBuilder<TypeBase, Int
1818
private static readonly bool UseOldBehavior34201 =
1919
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue34201", out var enabled34201) && enabled34201;
2020

21+
internal static readonly bool UseOldBehavior29997 =
22+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue29997", out var enabled29997) && enabled29997;
23+
2124
/// <summary>
2225
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
2326
/// 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
754757
{
755758
if (conflictingProperty.GetConfigurationSource() != ConfigurationSource.Explicit)
756759
{
757-
conflictingProperty.DeclaringType.RemoveProperty(conflictingProperty);
760+
if (UseOldBehavior29997)
761+
{
762+
conflictingProperty.DeclaringType.RemoveProperty(conflictingProperty);
763+
}
764+
else
765+
{
766+
conflictingProperty.DeclaringType.Builder.RemoveProperty(conflictingProperty, configurationSource);
767+
}
758768
}
759769
}
760770

test/EFCore.Cosmos.Tests/ModelBuilding/CosmosModelBuilderGenericTest.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,28 +1023,20 @@ public virtual void Reference_type_is_discovered_as_owned()
10231023
{
10241024
var modelBuilder = CreateModelBuilder();
10251025

1026-
modelBuilder.Entity<OneToOneOwnerWithField>(
1027-
e =>
1028-
{
1029-
e.Property(p => p.Id);
1030-
e.Property(p => p.AlternateKey);
1031-
e.Property(p => p.Description);
1032-
e.HasKey(p => p.Id);
1033-
});
1026+
modelBuilder.Entity<OwnerOfOwnees>();
10341027

10351028
var model = modelBuilder.FinalizeModel();
10361029

1037-
var owner = model.FindEntityType(typeof(OneToOneOwnerWithField));
1038-
Assert.Equal(typeof(OneToOneOwnerWithField).FullName, owner.Name);
1039-
var ownership = owner.FindNavigation(nameof(OneToOneOwnerWithField.OwnedDependent)).ForeignKey;
1030+
var owner = model.FindEntityType(typeof(OwnerOfOwnees))!;
1031+
var ownership = owner.FindNavigation(nameof(OwnerOfOwnees.Ownee1))!.ForeignKey;
10401032
Assert.True(ownership.IsOwnership);
1041-
Assert.Equal(nameof(OneToOneOwnerWithField.OwnedDependent), ownership.PrincipalToDependent.Name);
1042-
Assert.Equal(nameof(OneToOneOwnedWithField.OneToOneOwner), ownership.DependentToPrincipal.Name);
1043-
Assert.Equal(nameof(OneToOneOwnerWithField.Id), ownership.PrincipalKey.Properties.Single().Name);
1033+
Assert.Equal(nameof(OwnerOfOwnees.Ownee1), ownership.PrincipalToDependent!.Name);
1034+
Assert.Equal(nameof(Ownee1.Owner), ownership.DependentToPrincipal!.Name);
1035+
Assert.Equal(nameof(OwnerOfOwnees.Id), ownership.PrincipalKey.Properties.Single().Name);
10441036
var owned = ownership.DeclaringEntityType;
10451037
Assert.Single(owned.GetForeignKeys());
1046-
Assert.NotNull(model.FindEntityType(typeof(OneToOneOwnedWithField)));
1047-
Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(OneToOneOwnedWithField)));
1038+
Assert.NotNull(model.FindEntityType(typeof(Ownee1)));
1039+
Assert.Equal(1, model.GetEntityTypes().Count(e => e.ClrType == typeof(Ownee1)));
10481040
}
10491041

10501042
protected override TestModelBuilder CreateModelBuilder(Action<ModelConfigurationBuilder> configure = null)

test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ public virtual void Can_call_Owner_fluent_api_after_calling_Entity()
559559
modelBuilder.Owned<Ownee1>();
560560
modelBuilder.Owned<Ownee2>();
561561
modelBuilder.Owned<Ownee3>();
562+
563+
var model = modelBuilder.FinalizeModel();
564+
565+
var owner = model.FindEntityType(typeof(OwnerOfOwnees))!;
566+
var ownership = owner.FindNavigation(nameof(OwnerOfOwnees.Ownee1))!.ForeignKey;
567+
Assert.True(ownership.IsOwnership);
562568
}
563569

564570
[Flags]

test/EFCore.Tests/ModelBuilding/TestModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,16 +1144,23 @@ protected class OwnerOfOwnees
11441144

11451145
protected class Ownee1
11461146
{
1147+
public string Data { get; private set; } = "";
1148+
1149+
public OwnerOfOwnees Owner { get; private set; } = null!;
11471150
public Ownee3? NewOwnee3 { get; private set; }
11481151
}
11491152

11501153
protected class Ownee2
11511154
{
1155+
public Guid Data { get; private set; }
1156+
11521157
public Ownee3? Ownee3 { get; private set; }
11531158
}
11541159

11551160
protected class Ownee3
11561161
{
1162+
public DateTime Data { get; private set; }
1163+
11571164
public string? Name { get; private set; }
11581165
}
11591166

@@ -1221,6 +1228,7 @@ protected class OneToManyOwnedWithField
12211228
public OneToManyOwnerWithField? OneToManyOwner { get; set; }
12221229
}
12231230

1231+
[Index(nameof(OwnedDependent))]
12241232
protected class OneToOneOwnerWithField
12251233
{
12261234
public int Id;

0 commit comments

Comments
 (0)