Skip to content

Commit

Permalink
Remove referencing indexes when reconfiguring a property as a navigation
Browse files Browse the repository at this point in the history
Fixes #29997
  • Loading branch information
AndriySvyryd committed Sep 10, 2024
1 parent 36e4637 commit 58cb4ca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Internal/InternalTypeBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1251,28 +1251,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
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,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
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

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

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

0 comments on commit 58cb4ca

Please sign in to comment.