Skip to content

Commit

Permalink
Always store join table schema in the snapshot.
Browse files Browse the repository at this point in the history
Fixes #23937
  • Loading branch information
AndriySvyryd committed Sep 20, 2021
1 parent c6a9f1c commit ccde2d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}
Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,8 +1328,13 @@ public virtual void Many_to_many_join_table_stored_in_snapshot()
{
builder
.Entity<ManyToManyLeft>()
.ToTable("ManyToManyLeft", "schema")
.HasMany(l => l.Rights)
.WithMany(r => r.Lefts);
builder
.Entity<ManyToManyRight>()
.ToTable("ManyToManyRight", "schema");
},
AddBoilerPlate(
GetHeading()
Expand All @@ -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 =>
Expand All @@ -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 =>
Expand All @@ -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 =>
Expand Down Expand Up @@ -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());
});
}

Expand Down

0 comments on commit ccde2d5

Please sign in to comment.