Skip to content

Commit

Permalink
[release/7.0] Take schema into account when comparing FK constraints. (
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Jan 13, 2023
1 parent 9cbf778 commit 00e218a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
// Sealed for perf
public sealed class ForeignKeyConstraintComparer : IEqualityComparer<IForeignKeyConstraint>, IComparer<IForeignKeyConstraint>
{
private static readonly bool QuirkEnabled29741
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue29741", out var enabled) && enabled;

private ForeignKeyConstraintComparer()
{
}
Expand Down Expand Up @@ -66,7 +69,24 @@ public int Compare(IForeignKeyConstraint? x, IForeignKeyConstraint? y)
}

result = StringComparer.Ordinal.Compare(x.PrincipalTable.Name, y.PrincipalTable.Name);
return result != 0 ? result : StringComparer.Ordinal.Compare(x.Table.Name, y.Table.Name);
if (result != 0)
{
return result;
}

result = StringComparer.Ordinal.Compare(x.Table.Name, y.Table.Name);
if (result != 0 || QuirkEnabled29741)
{
return result;
}

result = StringComparer.Ordinal.Compare(x.PrincipalTable.Schema, y.PrincipalTable.Schema);
if (result != 0)
{
return result;
}

return result != 0 ? result : StringComparer.Ordinal.Compare(x.Table.Schema, y.Table.Schema);
}

/// <summary>
Expand Down
25 changes: 25 additions & 0 deletions test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,31 @@ public void Can_use_relational_model_with_keyless_TPH()
Assert.True(specialtyColumn.IsNullable);
}

[ConditionalFact]
public void Can_use_relational_model_with_tables_in_different_schemas()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Ignore<Order>();
modelBuilder.Entity<OrderDetails>(
cb =>
{
cb.HasKey(b => b.OrderId);
cb.OwnsOne(d => d.BillingAddress, a => a.ToTable("Details", "Billing"));
cb.OwnsOne(d => d.ShippingAddress, a => a.ToTable("Details", "Shipping"));
cb.OwnsOne(d => d.DateDetails, a => a.ToTable("Details", "Date"));
});

var model = Finalize(modelBuilder);

Assert.Equal(4, model.Model.GetEntityTypes().Count());
Assert.Empty(model.Views);

var orderDetails = model.Model.FindEntityType(typeof(OrderDetails));
var orderDetailsTable = orderDetails.GetTableMappings().Single().Table;
Assert.Equal(3, orderDetailsTable.ReferencingForeignKeyConstraints.Count());
}

[ConditionalFact]
public void Can_use_relational_model_with_SQL_queries()
{
Expand Down

0 comments on commit 00e218a

Please sign in to comment.