From 5131e8d1e929e4faacf7e93434ee6ddacd68e946 Mon Sep 17 00:00:00 2001 From: Brice Lambson Date: Wed, 2 Oct 2019 15:55:24 -0700 Subject: [PATCH] Migrations: Fix bug in complex column ordering code Fixes #14027 --- .../Internal/MigrationsModelDiffer.cs | 1 + .../Internal/MigrationsModelDifferTest.cs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs index ade0ec9b7da..98a50cbe853 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs @@ -732,6 +732,7 @@ private static IEnumerable GetSortedProperties(IEntityType entityType .Where( fk => fk.DeclaringEntityType.GetRootType() != entityType.GetRootType() && fk.DeclaringEntityType.GetTableName() == entityType.GetTableName() + && fk.DeclaringEntityType.GetSchema() == entityType.GetSchema() && fk == fk.DeclaringEntityType .FindForeignKey( fk.DeclaringEntityType.FindPrimaryKey().Properties, diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs index e6c36fc88a7..9bdd83de00c 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs @@ -7898,6 +7898,56 @@ public Blog Blog } } + [ConditionalFact] + public void Create_table_handles_same_name_but_different_schemas_and_identifying_relationship() + { + Execute( + (ModelBuilder _) => { }, + modelBuilder => modelBuilder + .Entity( + "Entity1", + x => + { + x.ToTable("Entity"); + + x.Property("Id"); + x.Property("Property1"); + }) + .Entity( + "Entity2", + x => + { + x.ToTable("Entity", "other"); + + x.Property("Id"); + x.Property("Property2"); + + x.HasOne("Entity1", null).WithMany().HasForeignKey("Id"); + }), + operations => + { + Assert.Equal(3, operations.Count); + + Assert.IsType(operations[0]); + + var operation1 = Assert.IsType(operations[1]); + Assert.Equal("Entity", operation1.Name); + Assert.Null(operation1.Schema); + Assert.Collection( + operation1.Columns, + x => Assert.Equal("Id", x.Name), + x => Assert.Equal("Property1", x.Name)); + + var operation2 = Assert.IsType(operations[2]); + Assert.Equal("Entity", operation2.Name); + Assert.Equal("other", operation2.Schema); + Assert.Collection( + operation2.Columns, + x => Assert.Equal("Id", x.Name), + x => Assert.Equal("Property2", x.Name)); + }); + } + protected override TestHelpers TestHelpers => RelationalTestHelpers.Instance; } }