Skip to content

Commit

Permalink
Use unmapped FKs for topological order when propagating store-generat…
Browse files Browse the repository at this point in the history
…ed values

Fixes #28654
  • Loading branch information
AndriySvyryd authored Aug 30, 2022
1 parent dce042c commit 99f03c7
Show file tree
Hide file tree
Showing 86 changed files with 2,083 additions and 894 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1375,22 +1375,13 @@ public static IEnumerable<IReadOnlyForeignKey> FindRowInternalForeignKeys(

foreach (var foreignKey in entityType.GetForeignKeys())
{
if (!foreignKey.PrincipalKey.IsPrimaryKey()
|| foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType)
|| !foreignKey.Properties.SequenceEqual(primaryKey.Properties)
|| !IsMapped(foreignKey, storeObject))
if (!foreignKey.IsRowInternal(storeObject))
{
continue;
}

yield return foreignKey;
}

static bool IsMapped(IReadOnlyForeignKey foreignKey, StoreObjectIdentifier storeObject)
=> (StoreObjectIdentifier.Create(foreignKey.DeclaringEntityType, storeObject.StoreObjectType) == storeObject
|| foreignKey.DeclaringEntityType.GetMappingFragments(storeObject.StoreObjectType).Any(f => f.StoreObject == storeObject))
&& (StoreObjectIdentifier.Create(foreignKey.PrincipalEntityType, storeObject.StoreObjectType) == storeObject
|| foreignKey.PrincipalEntityType.GetMappingFragments(storeObject.StoreObjectType).Any(f => f.StoreObject == storeObject));
}

/// <summary>
Expand Down
34 changes: 34 additions & 0 deletions src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,40 @@ public static IEnumerable<IForeignKeyConstraint> GetMappedConstraints(this IFore
return rootForeignKey == foreignKey ? null : rootForeignKey;
}

/// <summary>
/// Returns a value indicating whether this foreign key is between two entity types
/// sharing the same table-like store object.
/// </summary>
/// <param name="foreignKey">The foreign key.</param>
/// <param name="storeObject">The identifier of the store object.</param>
public static bool IsRowInternal(
this IReadOnlyForeignKey foreignKey,
StoreObjectIdentifier storeObject)
{
var entityType = foreignKey.DeclaringEntityType;
var primaryKey = entityType.FindPrimaryKey();
if (primaryKey == null || entityType.IsMappedToJson())
{
return false;
}

if (!foreignKey.PrincipalKey.IsPrimaryKey()
|| foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType)
|| !foreignKey.Properties.SequenceEqual(primaryKey.Properties)
|| !IsMapped(foreignKey, storeObject))
{
return false;
}

return true;

static bool IsMapped(IReadOnlyForeignKey foreignKey, StoreObjectIdentifier storeObject)
=> (StoreObjectIdentifier.Create(foreignKey.DeclaringEntityType, storeObject.StoreObjectType) == storeObject
|| foreignKey.DeclaringEntityType.GetMappingFragments(storeObject.StoreObjectType).Any(f => f.StoreObject == storeObject))
&& (StoreObjectIdentifier.Create(foreignKey.PrincipalEntityType, storeObject.StoreObjectType) == storeObject
|| foreignKey.PrincipalEntityType.GetMappingFragments(storeObject.StoreObjectType).Any(f => f.StoreObject == storeObject));
}

/// <summary>
/// <para>
/// Finds the first <see cref="IMutableForeignKey" /> that is mapped to the same constraint in a shared table-like object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public StoredProcedureParameterBuilder(
/// </summary>
public virtual IMutableStoredProcedureParameter Metadata
=> Builder.Metadata;

/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IConventionStoredProcedureParameter : IReadOnlyStoredProcedureP
/// </summary>
/// <exception cref="InvalidOperationException">If the stored procedure parameter has been removed from the model.</exception>
new IConventionStoredProcedureParameterBuilder Builder { get; }

/// <summary>
/// Sets the parameter name.
/// </summary>
Expand All @@ -33,7 +33,7 @@ public interface IConventionStoredProcedureParameter : IReadOnlyStoredProcedureP
/// </summary>
/// <returns>The configuration source for <see cref="IReadOnlyStoredProcedureParameter.Name" />.</returns>
ConfigurationSource? GetNameConfigurationSource();

/// <summary>
/// Sets the direction of the parameter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public interface IMutableStoredProcedureParameter : IReadOnlyStoredProcedurePara
/// Gets the stored procedure to which this parameter belongs.
/// </summary>
new IMutableStoredProcedure StoredProcedure { get; }

/// <summary>
/// Gets or sets the parameter name.
/// </summary>
new string Name { get; set; }

/// <summary>
/// Gets or sets the direction of the parameter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IMutableStoredProcedureResultColumn : IReadOnlyStoredProcedureR
/// Gets the stored procedure to which this result column belongs.
/// </summary>
new IMutableStoredProcedure StoredProcedure { get; }

/// <summary>
/// Gets or sets the result column name.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/IReadOnlyStoredProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface IReadOnlyStoredProcedure : IReadOnlyAnnotatable
{
return StoreObjectIdentifier.DeleteStoredProcedure(name, Schema);
}

if (EntityType.GetUpdateStoredProcedure() == this)
{
return StoreObjectIdentifier.UpdateStoredProcedure(name, Schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IReadOnlyStoredProcedureParameter : IReadOnlyAnnotatable
/// Gets the stored procedure to which this parameter belongs.
/// </summary>
IReadOnlyStoredProcedure StoredProcedure { get; }

/// <summary>
/// Gets the parameter name.
/// </summary>
Expand All @@ -25,7 +25,7 @@ public interface IReadOnlyStoredProcedureParameter : IReadOnlyAnnotatable
/// Gets the name of property mapped to this parameter.
/// </summary>
string? PropertyName { get; }

/// <summary>
/// Gets the direction of the parameter.
/// </summary>
Expand Down
22 changes: 11 additions & 11 deletions src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MigrationsModelDiffer(
/// 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>
protected virtual IRowIdentityMapFactory RowIdentityMapFactory { get; }
protected virtual IRowIdentityMapFactory RowIdentityMapFactory { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -702,7 +702,7 @@ private static IEnumerable<IColumn> GetSortedColumns(ITable table)

Check.DebugAssert(columns.Count == 0, "columns is not empty");

// issue #28539
// issue #28539
// ideally we should inject JSON column in the place corresponding to the navigation that maps to it in the clr type
var jsonColumns = table.Columns.Where(x => x is JsonColumn).OrderBy(x => x.Name);

Expand Down Expand Up @@ -1743,7 +1743,7 @@ protected virtual void TrackData(
AddSeedData(sourceEntityType, _sourceIdentityMaps, EntityState.Deleted);
}
}

private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentityMap> identityMaps, EntityState initialState)
{
var sensitiveLoggingEnabled = CommandBatchPreparerDependencies.LoggingOptions.IsSensitiveDataLoggingEnabled;
Expand Down Expand Up @@ -1816,21 +1816,21 @@ private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentity
BuildValuesString(key),
table.SchemaQualifiedName));
}

throw new InvalidOperationException(
RelationalStrings.DuplicateSeedData(
entityType.DisplayName(),
table.SchemaQualifiedName));
}

command = existingCommand;
}
else
{
command = CommandBatchPreparerDependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(
new NonTrackedModificationCommandParameters(table, sensitiveLoggingEnabled));
command.EntityState = initialState;

identityMap.Add(key, command);
}

Expand Down Expand Up @@ -1914,7 +1914,7 @@ private void AddSeedData(IEntityType entityType, Dictionary<ITable, IRowIdentity
writeValue = writeValue
&& initialState != EntityState.Deleted
&& property.GetBeforeSaveBehavior() == PropertySaveBehavior.Save;

command.AddColumnModification(
new ColumnModificationParameters(
column, originalValue: value, value, property, columnMapping.TypeMapping,
Expand Down Expand Up @@ -1982,7 +1982,7 @@ protected virtual void DiffData(
{
return;
}

var tableMapping = new Dictionary<ITable, (ITable, IRowIdentityMap)?>();
var unchangedColumns = new List<IColumnModification>();
var overridenColumns = new List<IColumnModification>();
Expand Down Expand Up @@ -2049,10 +2049,10 @@ protected virtual void DiffData(
{
targetRow.EntityState = EntityState.Unchanged;
}

continue;
}

if (sourceTable.IsExcludedFromMigrations
|| targetTable.IsExcludedFromMigrations)
{
Expand Down Expand Up @@ -2162,7 +2162,7 @@ protected virtual IEnumerable<MigrationOperation> GetDataOperations(
DiffContext diffContext)
{
TrackData(source, target, diffContext);

DiffData(source, target, diffContext);

var dataOperations = GetDataOperations(forSource: true, diffContext)
Expand Down
Loading

0 comments on commit 99f03c7

Please sign in to comment.