/// <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> /// Returns the default database schema that would be used for this entity type. /// </summary> /// <param name="entityType">The entity type to get the schema for.</param> /// <returns>The default database schema to which the entity type would be mapped.</returns> public static string?GetDefaultSchema(this IReadOnlyEntityType entityType) { var ownership = entityType.FindOwnership(); if (ownership != null) { return(ownership.PrincipalEntityType.GetSchema()); } var skipNavigationSchema = entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations()) .FirstOrDefault(n => !n.IsOnDependent) ?.DeclaringEntityType.GetSchema(); if (skipNavigationSchema != null && entityType.GetForeignKeys().SelectMany(fk => fk.GetReferencingSkipNavigations()) .Where(n => !n.IsOnDependent) .All(n => n.DeclaringEntityType.GetSchema() == skipNavigationSchema)) { return(skipNavigationSchema); } return(entityType.Model.GetDefaultSchema()); }