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

Add the column type to the migration when deleting a data using a removed column #22346

Merged
merged 1 commit into from
Sep 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,24 @@ protected virtual void Generate(

builder.AppendLine(",");

if (operation.KeyColumnTypes != null)
{
if (operation.KeyColumnTypes.Length == 1)
{
builder
.Append("keyColumnType: ")
.Append(Code.Literal(operation.KeyColumnTypes[0]));
}
else
{
builder
.Append("keyColumnTypes: ")
.Append(Code.Literal(operation.KeyColumnTypes));
}

builder.AppendLine(",");
}

if (operation.KeyValues.GetLength(0) == 1
&& operation.KeyValues.GetLength(1) == 1)
{
Expand Down
36 changes: 36 additions & 0 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ protected virtual IEnumerable<MigrationOperation> Remove([NotNull] IColumn sourc
};
operation.AddAnnotations(MigrationsAnnotations.ForRemove(source));

diffContext.AddDrop(source, operation);

yield return operation;
}

Expand Down Expand Up @@ -2260,11 +2262,20 @@ 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);

yield return new DeleteDataOperation
{
Schema = command.Schema,
Table = command.TableName,
KeyColumns = command.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
KeyColumnTypes = anyKeyColumnDropped
? keyColumns.Select(col => col.StoreType).ToArray()
: null,
KeyValues = ToMultidimensionalArray(
command.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToArray()),
IsDestructiveChange = true
Expand Down Expand Up @@ -2565,6 +2576,9 @@ private readonly IDictionary<ITable, CreateTableOperation> _createTableOperation
private readonly IDictionary<ITable, DropTableOperation> _dropTableOperations
= new Dictionary<ITable, DropTableOperation>();

private readonly IDictionary<IColumn, DropColumnOperation> _dropColumnOperations
= new Dictionary<IColumn, DropColumnOperation>();

private readonly IDictionary<DropTableOperation, ITable> _removedTables
= new Dictionary<DropTableOperation, ITable>();

Expand Down Expand Up @@ -2601,6 +2615,17 @@ public virtual void AddDrop([NotNull] ITable source, [NotNull] DropTableOperatio
_removedTables.Add(operation, source);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void AddDrop([NotNull] IColumn source, [NotNull] DropColumnOperation operation)
{
_dropColumnOperations.Add(source, operation);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -2660,6 +2685,17 @@ public virtual DropTableOperation FindDrop([NotNull] ITable source)
? operation
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual DropColumnOperation FindDrop([NotNull] IColumn source)
=> _dropColumnOperations.TryGetValue(source, out var operation)
? operation
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Loading