Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0.1] Generate the correct overload for excluding tables for owned types. #23178

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,17 @@ protected virtual void GenerateEntityTypeAnnotations(
{
if (((bool?)isExcludedAnnotation.Value) == true)
{
stringBuilder
.Append(", t => t.ExcludeFromMigrations()");
if (entityType.IsOwned())
{
// Issue #23173
stringBuilder
.Append(", excludedFromMigrations: true");
}
else
{
stringBuilder
.Append(", t => t.ExcludeFromMigrations()");
}
}

annotations.Remove(isExcludedAnnotation.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ public static EntityTypeBuilder<TEntity> ToTable<TEntity>(
public static OwnedNavigationBuilder ToTable(
[NotNull] this OwnedNavigationBuilder referenceOwnershipBuilder,
[CanBeNull] string name)
=> referenceOwnershipBuilder.ToTable(name, excludedFromMigrations: false);
=> ToTable(referenceOwnershipBuilder, name, schema: null, excludedFromMigrations:
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled
? false
: (bool?)null);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand Down Expand Up @@ -226,7 +229,10 @@ public static OwnedNavigationBuilder<TEntity, TRelatedEntity> ToTable<TEntity, T
where TEntity : class
where TRelatedEntity : class
=> (OwnedNavigationBuilder<TEntity, TRelatedEntity>)ToTable(
(OwnedNavigationBuilder)referenceOwnershipBuilder, name, excludedFromMigrations: false);
referenceOwnershipBuilder, name, schema: null, excludedFromMigrations:
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled
? false
: (bool?)null);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand Down Expand Up @@ -257,7 +263,10 @@ public static OwnedNavigationBuilder ToTable(
[NotNull] this OwnedNavigationBuilder referenceOwnershipBuilder,
[CanBeNull] string name,
[CanBeNull] string schema)
=> referenceOwnershipBuilder.ToTable(name, schema, excludedFromMigrations: false);
=> ToTable(referenceOwnershipBuilder, name, schema, excludedFromMigrations:
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled
? false
: (bool?)null);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand All @@ -277,9 +286,22 @@ public static OwnedNavigationBuilder ToTable(
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));

return ToTable(referenceOwnershipBuilder, name, schema, (bool?)excludedFromMigrations);
}

private static OwnedNavigationBuilder ToTable(
OwnedNavigationBuilder referenceOwnershipBuilder,
string name,
string schema,
bool? excludedFromMigrations)
{
referenceOwnershipBuilder.OwnedEntityType.SetTableName(name);
referenceOwnershipBuilder.OwnedEntityType.SetSchema(schema);
referenceOwnershipBuilder.OwnedEntityType.SetIsTableExcludedFromMigrations(excludedFromMigrations);

if (excludedFromMigrations.HasValue)
{
referenceOwnershipBuilder.OwnedEntityType.SetIsTableExcludedFromMigrations(excludedFromMigrations.Value);
}

return referenceOwnershipBuilder;
}
Expand All @@ -300,7 +322,10 @@ public static OwnedNavigationBuilder<TEntity, TRelatedEntity> ToTable<TEntity, T
where TEntity : class
where TRelatedEntity : class
=> (OwnedNavigationBuilder<TEntity, TRelatedEntity>)ToTable(
(OwnedNavigationBuilder)referenceOwnershipBuilder, name, schema, excludedFromMigrations: false);
referenceOwnershipBuilder, name, schema, excludedFromMigrations:
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled
? false
: (bool?)null);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ public static bool IsTableExcludedFromMigrations([NotNull] this IEntityType enti
return excluded.Value;
}

if (entityType.FindAnnotation(RelationalAnnotationNames.TableName) != null)
var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled;
if (useOldBehavior && entityType.FindAnnotation(RelationalAnnotationNames.TableName) != null)
{
return false;
}
Expand Down
13 changes: 10 additions & 3 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,12 @@ protected virtual IEnumerable<MigrationOperation> Remove([NotNull] IForeignKeyCo
{
var sourceTable = source.Table;

var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled;
if (!useOldBehavior && sourceTable.IsExcludedFromMigrations)
{
yield break;
}

var dropTableOperation = diffContext.FindDrop(sourceTable);
if (dropTableOperation == null)
{
Expand Down Expand Up @@ -2173,7 +2179,10 @@ private IEnumerable<MigrationOperation> GetDataOperations(
InsertDataOperation batchInsertOperation = null;
foreach (var command in commandBatch.ModificationCommands)
{
if (diffContext.FindDrop(model.FindTable(command.TableName, command.Schema)) != null)
var table = model.FindTable(command.TableName, command.Schema);
var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23137", out var isEnabled) && isEnabled;
if (diffContext.FindDrop(table) != null
|| (!useOldBehavior && table.IsExcludedFromMigrations))
{
continue;
}
Expand Down Expand Up @@ -2269,8 +2278,6 @@ private IEnumerable<MigrationOperation> GetDataOperations(
break;
}

var table = command.Entries.First().EntityType.GetTableMappings().Select(m => m.Table)
.First(t => t.Name == command.TableName && t.Schema == command.Schema);
var keyColumns = command.ColumnModifications.Where(col => col.IsKey)
.Select(c => table.FindColumn(c.ColumnName));
var anyKeyColumnDropped = keyColumns.Any(c => diffContext.FindDrop(c) != null);
Expand Down
Loading