/// <summary> /// Gets the foreign keys for the given entity type that point to other entity types /// sharing the same table-like store object. /// </summary> /// <param name="entityType">The entity type.</param> /// <param name="storeObject">The identifier of the store object.</param> public static IEnumerable <IReadOnlyForeignKey> FindRowInternalForeignKeys( this IReadOnlyEntityType entityType, StoreObjectIdentifier storeObject) { var primaryKey = entityType.FindPrimaryKey(); if (primaryKey == null) { yield break; } foreach (var foreignKey in entityType.GetForeignKeys()) { var principalEntityType = foreignKey.PrincipalEntityType; if (!foreignKey.PrincipalKey.IsPrimaryKey() || principalEntityType == foreignKey.DeclaringEntityType || !foreignKey.IsUnique #pragma warning disable EF1001 // Internal EF Core API usage. || !PropertyListComparer.Instance.Equals(foreignKey.Properties, primaryKey.Properties)) #pragma warning restore EF1001 // Internal EF Core API usage. { continue; } switch (storeObject.StoreObjectType) { case StoreObjectType.Table: if (storeObject.Name == principalEntityType.GetTableName() && storeObject.Schema == principalEntityType.GetSchema()) { yield return(foreignKey); } break; case StoreObjectType.View: if (storeObject.Name == principalEntityType.GetViewName() && storeObject.Schema == principalEntityType.GetViewSchema()) { yield return(foreignKey); } break; case StoreObjectType.Function: if (storeObject.Name == principalEntityType.GetFunctionName()) { yield return(foreignKey); } break; default: throw new NotSupportedException(storeObject.StoreObjectType.ToString()); } } }
/// <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 int Compare(string?x, string?y) { var xIndex = -1; var yIndex = -1; var properties = _entityType.FindPrimaryKey()?.Properties; if (properties != null) { for (var i = 0; i < properties.Count; i++) { var name = properties[i].Name; if (name == x) { xIndex = i; } if (name == y) { yIndex = i; } } } // Neither property is part of the Primary Key // Compare the property names if (xIndex == -1 && yIndex == -1) { return(StringComparer.Ordinal.Compare(x, y)); } // Both properties are part of the Primary Key // Compare the indices if (xIndex > -1 && yIndex > -1) { return(xIndex - yIndex); } // One property is part of the Primary Key // The primary key property is first return(xIndex > yIndex ? -1 : 1); }
public static IReadOnlyKey?FindDeclaredPrimaryKey([NotNull] this IReadOnlyEntityType entityType) => entityType.BaseType == null?entityType.FindPrimaryKey() : null;