Skip to content

Commit

Permalink
[release/8.0] Remove referencing indexes when reconfiguring a propert…
Browse files Browse the repository at this point in the history
…y as a navigation

Fixes #29997
  • Loading branch information
AndriySvyryd committed Sep 10, 2024
1 parent b2e366b commit 22e11eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public abstract class InternalTypeBaseBuilder : AnnotatableBuilder<TypeBase, Int
private static readonly bool UseOldBehavior34201 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue34201", out var enabled34201) && enabled34201;

internal static readonly bool UseOldBehavior29997 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue29997", out var enabled29997) && enabled29997;

/// <summary>
/// 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
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,28 +1023,20 @@ public virtual void Reference_type_is_discovered_as_owned()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<OneToOneOwnerWithField>(
e =>
{
e.Property(p => p.Id);
e.Property(p => p.AlternateKey);
e.Property(p => p.Description);
e.HasKey(p => p.Id);
});
modelBuilder.Entity<OwnerOfOwnees>();

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<ModelConfigurationBuilder> configure = null)
Expand Down
6 changes: 6 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,12 @@ public virtual void Can_call_Owner_fluent_api_after_calling_Entity()
modelBuilder.Owned<Ownee1>();
modelBuilder.Owned<Ownee2>();
modelBuilder.Owned<Ownee3>();

var model = modelBuilder.FinalizeModel();

var owner = model.FindEntityType(typeof(OwnerOfOwnees))!;
var ownership = owner.FindNavigation(nameof(OwnerOfOwnees.Ownee1))!.ForeignKey;
Assert.True(ownership.IsOwnership);
}

[Flags]
Expand Down
8 changes: 8 additions & 0 deletions test/EFCore.Tests/ModelBuilding/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down Expand Up @@ -1221,6 +1228,7 @@ protected class OneToManyOwnedWithField
public OneToManyOwnerWithField? OneToManyOwner { get; set; }
}

[Index(nameof(OwnedDependent))]
protected class OneToOneOwnerWithField
{
public int Id;
Expand Down

0 comments on commit 22e11eb

Please sign in to comment.