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 committed Aug 23, 2022
1 parent d610c20 commit 3532e2c
Show file tree
Hide file tree
Showing 76 changed files with 2,068 additions and 807 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
Loading

0 comments on commit 3532e2c

Please sign in to comment.