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 complex mapping support to the update pipeline #20674

Merged
merged 1 commit into from
Apr 19, 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 @@ -357,6 +357,7 @@ public static EntityTypeBuilder HasCheckConstraint(
{
if (constraint.Sql == sql)
{
((CheckConstraint)constraint).UpdateConfigurationSource(ConfigurationSource.Explicit);
return entityTypeBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static IConventionDbFunctionBuilder HasDbFunction(
var dbFunction = modelBuilder.Metadata.FindDbFunction(methodInfo);
if (dbFunction == null)
{
dbFunction = modelBuilder.Metadata.AddDbFunction(methodInfo);
dbFunction = modelBuilder.Metadata.AddDbFunction(methodInfo, fromDataAnnotation);
}
else
{
Expand Down Expand Up @@ -357,7 +357,7 @@ public static IConventionModelBuilder HasDefaultSchema(
{
if (modelBuilder.CanSetDefaultSchema(schema, fromDataAnnotation))
{
modelBuilder.Metadata.SetDefaultSchema(schema);
modelBuilder.Metadata.SetDefaultSchema(schema, fromDataAnnotation);

return modelBuilder;
}
Expand Down Expand Up @@ -400,7 +400,7 @@ public static IConventionModelBuilder HasMaxIdentifierLength(
{
if (modelBuilder.CanSetMaxIdentifierLength(length, fromDataAnnotation))
{
modelBuilder.Metadata.SetMaxIdentifierLength(length);
modelBuilder.Metadata.SetMaxIdentifierLength(length, fromDataAnnotation);

return modelBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand All @@ -29,13 +31,14 @@ public static OwnedNavigationBuilder HasCheckConstraint(
Check.NotEmpty(name, nameof(name));
Check.NullButNotEmpty(sql, nameof(sql));

var entityType = ownedNavigationBuilder.Metadata.DeclaringEntityType;
var entityType = ownedNavigationBuilder.OwnedEntityType;

var constraint = entityType.FindCheckConstraint(name);
if (constraint != null)
{
if (constraint.Sql == sql)
{
((CheckConstraint)constraint).UpdateConfigurationSource(ConfigurationSource.Explicit);
return ownedNavigationBuilder;
}

Expand Down
32 changes: 22 additions & 10 deletions src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,6 @@ public static IEnumerable<IColumnMapping> GetTableColumnMappings([NotNull] this
(IEnumerable<IColumnMapping>)property[RelationalAnnotationNames.TableColumnMappings]
?? Enumerable.Empty<IColumnMapping>();

/// <summary>
/// Returns the view or table columns to which the property is mapped.
/// </summary>
/// <param name="property"> The property. </param>
/// <returns> The view or table columns to which the property is mapped. </returns>
public static IEnumerable<IColumnMappingBase> GetViewOrTableColumnMappings([NotNull] this IProperty property) =>
(IEnumerable<IColumnMappingBase>)(property[RelationalAnnotationNames.ViewColumnMappings]
?? property[RelationalAnnotationNames.TableColumnMappings])
?? Enumerable.Empty<IColumnMappingBase>();

/// <summary>
/// Returns the view columns to which the property is mapped.
/// </summary>
Expand All @@ -314,6 +304,28 @@ public static IEnumerable<IViewColumnMapping> GetViewColumnMappings([NotNull] th
(IEnumerable<IViewColumnMapping>)property[RelationalAnnotationNames.ViewColumnMappings]
?? Enumerable.Empty<IViewColumnMapping>();

/// <summary>
/// Returns the table column corresponding to this property if it's mapped to the given table.
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="tableName"> The target table name. </param>
/// <param name="schema"> The target table schema. </param>
/// <returns> The table column to which the property is mapped. </returns>
public static IColumn FindTableColumn([NotNull] this IProperty property, [NotNull] string tableName, [CanBeNull] string schema)
=> property.GetTableColumnMappings().Select(m => m.Column)
.FirstOrDefault(c => c.Table.Name == tableName && c.Table.Schema == schema);

/// <summary>
/// Returns the view column corresponding to this property if it's mapped to the given table.
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="viewName"> The target table name. </param>
/// <param name="schema"> The target table schema. </param>
/// <returns> The table column to which the property is mapped. </returns>
public static IViewColumn FindViewColumn([NotNull] this IProperty property, [NotNull] string viewName, [CanBeNull] string schema)
=> property.GetViewColumnMappings().Select(m => m.Column)
.FirstOrDefault(c => c.View.Name == viewName && c.View.Schema == schema);

/// <summary>
/// Returns the SQL expression that is used as the default value for the column this property is mapped to.
/// </summary>
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Relational/Metadata/Internal/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ public Sequence(
_name = name;
_schema = schema;
_configurationSource = configurationSource;
#pragma warning disable EF1001 // Internal EF Core API usage.
Builder = new InternalSequenceBuilder(this, ((Model)model).Builder.ModelBuilder);
#pragma warning restore EF1001 // Internal EF Core API usage.
Builder = new InternalSequenceBuilder(this, ((IConventionModel)model).Builder);
}

/// <summary>
Expand All @@ -137,9 +135,7 @@ public Sequence([NotNull] IModel model, [NotNull] string annotationName)
_maxValue = data.MaxValue;
_clrType = data.ClrType;
_isCyclic = data.IsCyclic;
#pragma warning disable EF1001 // Internal EF Core API usage.
Builder = new InternalSequenceBuilder(this, ((Model)model).Builder.ModelBuilder);
#pragma warning restore EF1001 // Internal EF Core API usage.
Builder = new InternalSequenceBuilder(this, ((IConventionModel)model).Builder);
}

/// <summary>
Expand Down
60 changes: 30 additions & 30 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,8 +1728,7 @@ protected virtual void DiffData(
}
}

var targetKeys = target.EntityTypeMappings
.SelectMany(m => m.EntityType.GetDeclaredKeys()).Where(k => k.IsPrimaryKey()).ToList();
var targetKeys = target.PrimaryKey?.MappedKeys.ToList() ?? new List<IKey>();
var keyMapping = new Dictionary<IEntityType,
Dictionary<IKey, List<(IProperty Property, ValueConverter SourceConverter, ValueConverter TargetConverter)>>>();
foreach (var sourceMapping in source.EntityTypeMappings)
Expand Down Expand Up @@ -1792,7 +1791,6 @@ protected virtual void DiffData(

if (!keyMapping.TryGetValue(sourceEntityType, out var targetKeyMap))
{
entryMapping.RecreateRow = true;
continue;
}

Expand Down Expand Up @@ -2021,20 +2019,25 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations([NotNull] Di
foreach (var commandBatch in commandBatches)
{
InsertDataOperation batchInsertOperation = null;
foreach (var c in commandBatch.ModificationCommands)
foreach (var command in commandBatch.ModificationCommands)
{
if (c.EntityState == EntityState.Added)
if (diffContext.FindDrop(model.FindTable(command.TableName, command.Schema)) != null)
{
continue;
}

if (command.EntityState == EntityState.Added)
{
if (batchInsertOperation != null)
{
if (batchInsertOperation.Table == c.TableName
&& batchInsertOperation.Schema == c.Schema
if (batchInsertOperation.Table == command.TableName
&& batchInsertOperation.Schema == command.Schema
&& batchInsertOperation.Columns.SequenceEqual(
c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(col => col.ColumnName)))
command.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(col => col.ColumnName)))
{
batchInsertOperation.Values =
AddToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(GetValue).ToList(),
command.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(GetValue).ToList(),
batchInsertOperation.Values);
continue;
}
Expand All @@ -2044,15 +2047,15 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations([NotNull] Di

batchInsertOperation = new InsertDataOperation
{
Schema = c.Schema,
Table = c.TableName,
Columns = c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(col => col.ColumnName)
Schema = command.Schema,
Table = command.TableName,
Columns = command.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(col => col.ColumnName)
.ToArray(),
Values = ToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(GetValue).ToList())
command.ColumnModifications.Where(col => col.IsKey || col.IsWrite).Select(GetValue).ToList())
};
}
else if (c.EntityState == EntityState.Modified)
else if (command.EntityState == EntityState.Modified)
{
if (batchInsertOperation != null)
{
Expand All @@ -2062,14 +2065,14 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations([NotNull] Di

yield return new UpdateDataOperation
{
Schema = c.Schema,
Table = c.TableName,
KeyColumns = c.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
Schema = command.Schema,
Table = command.TableName,
KeyColumns = command.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
KeyValues = ToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToList()),
Columns = c.ColumnModifications.Where(col => col.IsWrite).Select(col => col.ColumnName).ToArray(),
command.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToList()),
Columns = command.ColumnModifications.Where(col => col.IsWrite).Select(col => col.ColumnName).ToArray(),
Values = ToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsWrite).Select(GetValue).ToList())
command.ColumnModifications.Where(col => col.IsWrite).Select(GetValue).ToList())
};
}
else
Expand All @@ -2080,17 +2083,14 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations([NotNull] Di
batchInsertOperation = null;
}

if (diffContext.FindDrop(model.FindTable(c.TableName, c.Schema)) == null)
yield return new DeleteDataOperation
{
yield return new DeleteDataOperation
{
Schema = c.Schema,
Table = c.TableName,
KeyColumns = c.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
KeyValues = ToMultidimensionalArray(
c.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToArray())
};
}
Schema = command.Schema,
Table = command.TableName,
KeyColumns = command.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),
KeyValues = ToMultidimensionalArray(
command.ColumnModifications.Where(col => col.IsKey).Select(GetValue).ToArray())
};
}
}

Expand Down
Loading