Skip to content

Commit

Permalink
Don't stop ForeignKeyRequirednessChanged conventions prematurely
Browse files Browse the repository at this point in the history
Fixes #23555
  • Loading branch information
AndriySvyryd committed Dec 10, 2020
1 parent cec3f51 commit b1459fe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/EFCore/Metadata/Conventions/CascadeDeleteConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public virtual void ProcessForeignKeyRequirednessChanged(
IConventionContext<bool?> context)
{
var newRelationshipBuilder = relationshipBuilder.OnDelete(GetTargetDeleteBehavior(relationshipBuilder.Metadata));
context.StopProcessingIfChanged(newRelationshipBuilder?.Metadata.IsRequired);
if (newRelationshipBuilder != null)
{
context.StopProcessingIfChanged(newRelationshipBuilder.Metadata.IsRequired);
}
}

/// <summary>
Expand Down
50 changes: 48 additions & 2 deletions test/EFCore.Tests/ModelBuilding/OneToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -1130,7 +1129,54 @@ public virtual void Can_have_FK_semi_specified_with_explicit_PK()

expectedPrincipalProperties.Add(fk.PrincipalKey.Properties.Single());
AssertEqual(expectedPrincipalProperties, principalType.GetProperties());
expectedDependentProperties.Add(fk.Properties.Single());
var fkProperty = fk.Properties.Single();
Assert.False(fkProperty.IsNullable);
expectedDependentProperties.Add(fkProperty);
AssertEqual(expectedDependentProperties, dependentType.GetProperties());
Assert.Equal(dependentType.GetForeignKeys().Count(), dependentType.GetIndexes().Count());
Assert.False(fk.DeclaringEntityType.FindIndex(fk.Properties).IsUnique);
Assert.Empty(principalType.GetIndexes());
}

[ConditionalFact]
public virtual void Can_specify_requiredness_after_OnDelete()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;
modelBuilder.Ignore<OrderDetails>();
modelBuilder.Ignore<CustomerDetails>();
modelBuilder.Entity<Customer>();
modelBuilder.Entity<Order>();

var dependentType = model.FindEntityType(typeof(Order));
var principalType = model.FindEntityType(typeof(Customer));
var expectedPrincipalProperties = principalType.GetProperties().ToList();
var expectedDependentProperties = dependentType.GetProperties().ToList();

modelBuilder
.Entity<Order>().HasOne(e => e.Customer).WithMany(e => e.Orders)
.HasForeignKey("CustomerAlternateKey")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();

modelBuilder.FinalizeModel();

var fk = dependentType.GetForeignKeys().Single();

Assert.Equal("Customer", dependentType.GetNavigations().Single().Name);
Assert.Equal("Orders", principalType.GetNavigations().Single().Name);
Assert.Same(fk, dependentType.GetNavigations().Single().ForeignKey);
Assert.Same(fk, principalType.GetNavigations().Single().ForeignKey);
Assert.Empty(principalType.GetForeignKeys());

var principalKey = principalType.FindPrimaryKey();
Assert.Same(principalKey, fk.PrincipalKey);

expectedPrincipalProperties.Add(fk.PrincipalKey.Properties.Single());
AssertEqual(expectedPrincipalProperties, principalType.GetProperties());
var fkProperty = fk.Properties.Single();
Assert.False(fkProperty.IsNullable);
expectedDependentProperties.Add(fkProperty);
AssertEqual(expectedDependentProperties, dependentType.GetProperties());
Assert.Equal(dependentType.GetForeignKeys().Count(), dependentType.GetIndexes().Count());
Assert.False(fk.DeclaringEntityType.FindIndex(fk.Properties).IsUnique);
Expand Down

0 comments on commit b1459fe

Please sign in to comment.