Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove referenced key when removing the FK from skip navigation #22041

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/EFCore/Metadata/Builders/CollectionNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected virtual IMutableSkipNavigation WithLeftManyNavigation([NotNull] string

using (foreignKey.DeclaringEntityType.Model.ConventionDispatcher.DelayConventions())
{
foreignKey.DeclaringEntityType.RemoveForeignKey(foreignKey);
foreignKey.DeclaringEntityType.Builder.HasNoRelationship(foreignKey, ConfigurationSource.Explicit);
Builder = null;
return ((EntityType)DeclaringEntityType).Builder.HasSkipNavigation(
navigationMember,
Expand Down Expand Up @@ -347,7 +347,7 @@ private IMutableSkipNavigation WithRightManyNavigation(MemberIdentity navigation
{
if (conflictingNavigation != null)
{
foreignKey.DeclaringEntityType.RemoveForeignKey(foreignKey);
foreignKey.DeclaringEntityType.Builder.HasNoRelationship(foreignKey, ConfigurationSource.Explicit);
}
else
{
Expand Down
9 changes: 3 additions & 6 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@ public virtual InternalEntityTypeBuilder HasBaseType(
foreach (var key in Metadata.GetKeys())
{
if (key.ReferencingForeignKeys == null
|| !key.ReferencingForeignKeys.Any()
|| !key.Properties.Any(p => removedInheritedProperties.Contains(p)))
{
continue;
Expand Down Expand Up @@ -2124,12 +2125,8 @@ public static EntityType.Snapshot DetachAllMembers([NotNull] EntityType entityTy

private void RemoveKeyIfUnused(Key key, ConfigurationSource configurationSource = ConfigurationSource.Convention)
{
if (Metadata.FindPrimaryKey() == key)
{
return;
}

if (key.GetReferencingForeignKeys().Any())
if (Metadata.FindPrimaryKey() == key
|| key.ReferencingForeignKeys?.Any() == true)
{
return;
}
Expand Down
37 changes: 24 additions & 13 deletions src/EFCore/Metadata/Internal/InternalSkipNavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand Down Expand Up @@ -63,25 +64,35 @@ public InternalSkipNavigationBuilder([NotNull] SkipNavigation metadata, [NotNull
/// </summary>
public virtual InternalSkipNavigationBuilder HasForeignKey([CanBeNull] ForeignKey foreignKey, ConfigurationSource configurationSource)
{
if (CanSetForeignKey(foreignKey, configurationSource))
if (!CanSetForeignKey(foreignKey, configurationSource))
{
if (foreignKey != null)
return null;
}

if (foreignKey != null)
{
foreignKey.UpdateConfigurationSource(configurationSource);

if (Metadata.Inverse?.JoinEntityType != null
&& Metadata.Inverse.JoinEntityType
!= (Metadata.IsOnDependent ? foreignKey.PrincipalEntityType : foreignKey.DeclaringEntityType))
{
foreignKey.UpdateConfigurationSource(configurationSource);

if (Metadata.Inverse?.JoinEntityType != null
&& Metadata.Inverse.JoinEntityType
!= (Metadata.IsOnDependent ? foreignKey.PrincipalEntityType : foreignKey.DeclaringEntityType))
{
Metadata.Inverse.Builder.HasForeignKey(null, configurationSource);
}
Metadata.Inverse.Builder.HasForeignKey(null, configurationSource);
}
}

Metadata.SetForeignKey(foreignKey, configurationSource);
return this;
var oldForeignKey = Metadata.ForeignKey;

Metadata.SetForeignKey(foreignKey, configurationSource);

if (oldForeignKey?.Builder != null
&& oldForeignKey != foreignKey
&& oldForeignKey.ReferencingSkipNavigations?.Any() != true)
{
oldForeignKey.DeclaringEntityType.Builder.HasNoRelationship(oldForeignKey, ConfigurationSource.Convention);
}

return null;
return this;
}

/// <summary>
Expand Down
23 changes: 13 additions & 10 deletions test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public virtual void Finds_existing_navigations_and_uses_associated_FK_with_field
var modelBuilder = CreateModelBuilder();
var model = (IModel)modelBuilder.Model;

modelBuilder.Entity<ManyToManyPrincipalWithField>()
.HasMany(p => p.Dependents)
.WithMany(d => d.ManyToManyPrincipals)
.UsingEntity<ManyToManyJoinWithFields>(
jwf => jwf.HasOne<DependentWithField>(j => j.DependentWithField)
.WithMany(),
jwf => jwf.HasOne<ManyToManyPrincipalWithField>(j => j.ManyToManyPrincipalWithField)
.WithMany())
.HasKey(j => new { j.DependentWithFieldId, j.ManyToManyPrincipalWithFieldId });

modelBuilder.Entity<ManyToManyPrincipalWithField>(e =>
{
e.Property(p => p.Id);
Expand All @@ -82,16 +92,6 @@ public virtual void Finds_existing_navigations_and_uses_associated_FK_with_field
e.HasKey(d => d.DependentWithFieldId);
});

modelBuilder.Entity<ManyToManyPrincipalWithField>()
.HasMany(p => p.Dependents)
.WithMany(d => d.ManyToManyPrincipals)
.UsingEntity<ManyToManyJoinWithFields>(
jwf => jwf.HasOne<DependentWithField>(j => j.DependentWithField)
.WithMany(),
jwf => jwf.HasOne<ManyToManyPrincipalWithField>(j => j.ManyToManyPrincipalWithField)
.WithMany())
.HasKey(j => new { j.DependentWithFieldId, j.ManyToManyPrincipalWithFieldId });

var principalEntityType = model.FindEntityType(typeof(ManyToManyPrincipalWithField));
var dependentEntityType = model.FindEntityType(typeof(DependentWithField));
var joinEntityType = model.FindEntityType(typeof(ManyToManyJoinWithFields));
Expand Down Expand Up @@ -119,6 +119,9 @@ public virtual void Finds_existing_navigations_and_uses_associated_FK_with_field

Assert.Same(principalToJoinNav, principalEntityType.GetSkipNavigations().Single());
Assert.Same(dependentToJoinNav, dependentEntityType.GetSkipNavigations().Single());
Assert.Single(principalEntityType.GetDeclaredKeys());
Assert.Single(dependentEntityType.GetDeclaredKeys());
Assert.Single(joinEntityType.GetDeclaredKeys());
Assert.Equal(2, joinEntityType.GetForeignKeys().Count());
Assert.Same(principalToDependentFk, joinEntityType.GetForeignKeys().Last());
Assert.Same(dependentToPrincipalFk, joinEntityType.GetForeignKeys().First());
Expand Down