From ccde2d531e41c3e8b65a85f525cddeeb7c8e682b Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Mon, 20 Sep 2021 10:22:30 -0700 Subject: [PATCH] Always store join table schema in the snapshot. Fixes #23937 --- .../Migrations/Design/CSharpSnapshotGenerator.cs | 13 +++++++------ .../Migrations/ModelSnapshotSqlServerTest.cs | 14 +++++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs b/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs index 404be88877f..e4219ce4863 100644 --- a/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs +++ b/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs @@ -770,14 +770,15 @@ protected virtual void GenerateEntityTypeAnnotations( if (tableName != null || tableNameAnnotation != null) { - var schemaAnnotation = annotations.Find(RelationalAnnotationNames.Schema); stringBuilder .AppendLine() .Append(entityTypeBuilderName) .Append(".ToTable("); + var schemaAnnotation = annotations.Find(RelationalAnnotationNames.Schema); + var schema = (string?)schemaAnnotation?.Value ?? entityType.GetSchema(); if (tableName == null - && (schemaAnnotation == null || schemaAnnotation.Value == null)) + && (schemaAnnotation == null || schema == null)) { stringBuilder.Append("(string)"); } @@ -790,19 +791,19 @@ protected virtual void GenerateEntityTypeAnnotations( } var isExcludedAnnotation = annotations.Find(RelationalAnnotationNames.IsTableExcludedFromMigrations); - if (schemaAnnotation != null - && (tableName != null || schemaAnnotation.Value != null)) + if (schema != null + || (schemaAnnotation != null && tableName != null)) { stringBuilder .Append(", "); - if (schemaAnnotation.Value == null + if (schema == null && ((bool?)isExcludedAnnotation?.Value) != true) { stringBuilder.Append("(string)"); } - stringBuilder.Append(Code.UnknownLiteral(schemaAnnotation.Value)); + stringBuilder.Append(Code.UnknownLiteral(schema)); } if (isExcludedAnnotation != null) diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index b562a7c4046..5a901cc7229 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -1328,8 +1328,13 @@ public virtual void Many_to_many_join_table_stored_in_snapshot() { builder .Entity() + .ToTable("ManyToManyLeft", "schema") .HasMany(l => l.Rights) .WithMany(r => r.Lefts); + + builder + .Entity() + .ToTable("ManyToManyRight", "schema"); }, AddBoilerPlate( GetHeading() @@ -1346,7 +1351,7 @@ public virtual void Many_to_many_join_table_stored_in_snapshot() b.HasIndex(""RightsId""); - b.ToTable(""ManyToManyLeftManyToManyRight""); + b.ToTable(""ManyToManyLeftManyToManyRight"", ""schema""); }); modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+ManyToManyLeft"", b => @@ -1362,7 +1367,7 @@ public virtual void Many_to_many_join_table_stored_in_snapshot() b.HasKey(""Id""); - b.ToTable(""ManyToManyLeft""); + b.ToTable(""ManyToManyLeft"", ""schema""); }); modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+ManyToManyRight"", b => @@ -1378,7 +1383,7 @@ public virtual void Many_to_many_join_table_stored_in_snapshot() b.HasKey(""Id""); - b.ToTable(""ManyToManyRight""); + b.ToTable(""ManyToManyRight"", ""schema""); }); modelBuilder.Entity(""ManyToManyLeftManyToManyRight"", b => @@ -1459,6 +1464,9 @@ public virtual void Many_to_many_join_table_stored_in_snapshot() Assert.Equal("RightsId", p.Name); }); }); + + Assert.Equal("ManyToManyLeftManyToManyRight", joinEntity.GetTableName()); + Assert.Equal("schema", joinEntity.GetSchema()); }); }