From 245f20b5bd246d3a75941d4510526666b0afd2ca Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Mon, 17 Aug 2020 19:13:44 -0700 Subject: [PATCH] Don't create an FK constraint if a derived type in TPT uses table sharing --- .../Metadata/Internal/RelationalModel.cs | 2 +- .../Internal/MigrationsModelDifferTest.cs | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs index ec56f0d010b..c3deafae37d 100644 --- a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs +++ b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs @@ -820,7 +820,7 @@ private static void PopulateConstraints(Table table) if (columns.SequenceEqual(principalColumns)) { // Principal and dependent properties are mapped to the same columns so the constraint is redundant - continue; + break; } constraint = new ForeignKeyConstraint(name, table, principalTable, columns, principalColumns, diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs index 4bb76d32403..545a6888617 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs @@ -1760,6 +1760,51 @@ public void Rename_property_and_column_when_snapshot() skipSourceConventions: true); } + [ConditionalFact] + public void Add_table_sharing_to_TPT() + { + Execute( + common => { + common.Entity( + "Order", + x => + { + x.ToTable("Order"); + x.Property("Id"); + }); + common.Entity( + "DetailedOrder", + x => + { + x.ToTable("DetailedOrder"); + x.HasBaseType("Order"); + x.Property("Description").HasColumnName("Description"); + }); + }, + _ => { }, + target => { + target.Entity( + "OrderDetails", + x => + { + x.ToTable("DetailedOrder"); + x.Property("Id"); + x.Property("Description").HasColumnName("Description"); + x.Property("OrderDate"); + x.HasOne("DetailedOrder", null).WithOne().HasForeignKey("OrderDetails", "Id"); + }); + }, + operations => + { + Assert.Equal(1, operations.Count); + + var operation = Assert.IsType(operations[0]); + Assert.Null(operation.Schema); + Assert.Equal("DetailedOrder", operation.Table); + Assert.Equal("OrderDate", operation.Name); + }); + } + [ConditionalFact] public void Rename_column_in_TPT_with_table_sharing_and_seed_data() {