Example #1
0
        public static IReadOnlyEntityType ResolveEntityTypeInHierarchy(
            this IReadOnlyForeignKey foreignKey, IReadOnlyEntityType entityType)
        {
            if (!foreignKey.DeclaringEntityType.IsAssignableFrom(entityType) &&
                !foreignKey.PrincipalEntityType.IsAssignableFrom(entityType))
            {
                throw new InvalidOperationException(
                          CoreStrings.EntityTypeNotInRelationship(
                              entityType.DisplayName(),
                              foreignKey.DeclaringEntityType.DisplayName(),
                              foreignKey.PrincipalEntityType.DisplayName()));
            }

            if (foreignKey.DeclaringEntityType.IsAssignableFrom(foreignKey.PrincipalEntityType) ||
                foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType))
            {
                throw new InvalidOperationException(
                          CoreStrings.IntraHierarchicalAmbiguousTargetEntityType(
                              entityType.DisplayName(), foreignKey.Properties.Format(),
                              foreignKey.PrincipalEntityType.DisplayName(),
                              foreignKey.DeclaringEntityType.DisplayName()));
            }

            return(foreignKey.DeclaringEntityType.IsAssignableFrom(entityType)
                ? foreignKey.DeclaringEntityType
                : foreignKey.PrincipalEntityType);
        }
Example #2
0
        public static string ToDebugString(
            [NotNull] this IReadOnlyForeignKey foreignKey,
            MetadataDebugStringOptions options,
            int indent = 0)
        {
            var builder      = new StringBuilder();
            var indentString = new string(' ', indent);

            builder.Append(indentString);

            var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0;

            if (singleLine)
            {
                builder.Append("ForeignKey: ");
            }

            builder
            .Append(foreignKey.DeclaringEntityType.DisplayName())
            .Append(" ")
            .Append(foreignKey.Properties.Format())
            .Append(" -> ")
            .Append(foreignKey.PrincipalEntityType.DisplayName())
            .Append(" ")
            .Append(foreignKey.PrincipalKey.Properties.Format());

            if (foreignKey.IsUnique)
            {
                builder.Append(" Unique");
            }

            if (foreignKey.IsOwnership)
            {
                builder.Append(" Ownership");
            }

            if (foreignKey.PrincipalToDependent != null)
            {
                builder.Append(" ToDependent: ").Append(foreignKey.PrincipalToDependent.Name);
            }

            if (foreignKey.DependentToPrincipal != null)
            {
                builder.Append(" ToPrincipal: ").Append(foreignKey.DependentToPrincipal.Name);
            }

            if (foreignKey.DeleteBehavior != DeleteBehavior.NoAction)
            {
                builder
                .Append(" ")
                .Append(foreignKey.DeleteBehavior);
            }

            if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0)
            {
                builder.Append(foreignKey.AnnotationsToDebugString(indent + 2));
            }

            return(builder.ToString());
        }
Example #3
0
        public static bool IsBaseLinking([NotNull] this IReadOnlyForeignKey foreignKey)
        {
            var primaryKey = foreignKey.DeclaringEntityType.FindPrimaryKey();

            return(primaryKey == foreignKey.PrincipalKey &&
                   foreignKey.Properties.SequenceEqual(primaryKey.Properties));
        }
Example #4
0
 /// <summary>
 ///     Constructs the event payload.
 /// </summary>
 /// <param name="eventDefinition">The event definition.</param>
 /// <param name="messageGenerator">A delegate that generates a log message for this event.</param>
 /// <param name="foreignKey">The foreign key.</param>
 public ForeignKeyEventData(
     EventDefinitionBase eventDefinition,
     Func <EventDefinitionBase, EventData, string> messageGenerator,
     IReadOnlyForeignKey foreignKey)
     : base(eventDefinition, messageGenerator)
 {
     ForeignKey = foreignKey;
 }
        /// <summary>
        ///     Returns the foreign key constraint name.
        /// </summary>
        /// <param name="foreignKey"> The foreign key. </param>
        /// <returns> The foreign key constraint name. </returns>
        public static string?GetConstraintName(this IReadOnlyForeignKey foreignKey)
        {
            var annotation = foreignKey.FindAnnotation(RelationalAnnotationNames.Name);

            return(annotation != null
                ? (string?)annotation.Value
                : foreignKey.GetDefaultName());
        }
Example #6
0
        /// <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 static IEnumerable <IReadOnlyNavigation> GetNavigations(this IReadOnlyForeignKey foreignKey)
        {
            if (foreignKey.PrincipalToDependent != null)
            {
                yield return(foreignKey.PrincipalToDependent);
            }

            if (foreignKey.DependentToPrincipal != null)
            {
                yield return(foreignKey.DependentToPrincipal);
            }
        }
Example #7
0
    /// <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 virtual string GetPrincipalEndCandidateNavigationPropertyName(
        IReadOnlyForeignKey foreignKey,
        string dependentEndNavigationPropertyName)
    {
        var allForeignKeysBetweenDependentAndPrincipal =
            foreignKey.PrincipalEntityType.GetReferencingForeignKeys()
            .Where(fk => foreignKey.DeclaringEntityType == fk.DeclaringEntityType);

        return(allForeignKeysBetweenDependentAndPrincipal?.Count() > 1
            ? foreignKey.DeclaringEntityType.ShortName()
               + dependentEndNavigationPropertyName
            : foreignKey.DeclaringEntityType.ShortName());
    }
Example #8
0
    /// <summary>
    ///     Returns the foreign key constraint name.
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <returns>The foreign key constraint name.</returns>
    public static string?GetConstraintName(this IReadOnlyForeignKey foreignKey)
    {
        var tableName = foreignKey.DeclaringEntityType.GetTableName();

        if (tableName == null)
        {
            return(null);
        }

        var annotation = foreignKey.FindAnnotation(RelationalAnnotationNames.Name);

        return(annotation != null
            ? (string?)annotation.Value
            : foreignKey.GetDefaultName());
    }
Example #9
0
        public static IReadOnlyEntityType GetRelatedEntityType(
            [NotNull] this IReadOnlyForeignKey foreignKey, [NotNull] IReadOnlyEntityType entityType)
        {
            if (foreignKey.DeclaringEntityType != entityType &&
                foreignKey.PrincipalEntityType != entityType)
            {
                throw new InvalidOperationException(
                          CoreStrings.EntityTypeNotInRelationshipStrict(
                              entityType.DisplayName(),
                              foreignKey.DeclaringEntityType.DisplayName(),
                              foreignKey.PrincipalEntityType.DisplayName()));
            }

            return(foreignKey.DeclaringEntityType == entityType
                ? foreignKey.PrincipalEntityType
                : foreignKey.DeclaringEntityType);
        }
Example #10
0
        /// <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 static IEnumerable <IReadOnlyNavigation> FindNavigationsTo(
            this IReadOnlyForeignKey foreignKey, IReadOnlyEntityType entityType)
        {
            if (foreignKey.DeclaringEntityType != entityType &&
                foreignKey.PrincipalEntityType != entityType)
            {
                throw new InvalidOperationException(
                          CoreStrings.EntityTypeNotInRelationshipStrict(
                              entityType.DisplayName(),
                              foreignKey.DeclaringEntityType.DisplayName(),
                              foreignKey.PrincipalEntityType.DisplayName()));
            }

            return(foreignKey.IsSelfReferencing()
                ? foreignKey.GetNavigations()
                : foreignKey.FindNavigations(foreignKey.PrincipalEntityType == entityType));
        }
Example #11
0
        /// <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 static IEnumerable <IReadOnlyNavigation> FindNavigationsToInHierarchy(
            this IReadOnlyForeignKey foreignKey,
            IReadOnlyEntityType entityType)
        {
            if (!foreignKey.DeclaringEntityType.IsAssignableFrom(entityType) &&
                !foreignKey.PrincipalEntityType.IsAssignableFrom(entityType))
            {
                throw new InvalidOperationException(
                          CoreStrings.EntityTypeNotInRelationship(
                              entityType.DisplayName(), foreignKey.DeclaringEntityType.DisplayName(),
                              foreignKey.PrincipalEntityType.DisplayName()));
            }

            return(foreignKey.DeclaringEntityType.IsAssignableFrom(foreignKey.PrincipalEntityType) ||
                   foreignKey.PrincipalEntityType.IsAssignableFrom(foreignKey.DeclaringEntityType)
                    ? foreignKey.GetNavigations()
                    : foreignKey.FindNavigations(foreignKey.PrincipalEntityType.IsAssignableFrom(entityType)));
        }
Example #12
0
 private static IEnumerable <IReadOnlyNavigation> FindNavigations(
     this IReadOnlyForeignKey foreignKey,
     bool toPrincipal)
 {
     if (toPrincipal)
     {
         if (foreignKey.DependentToPrincipal != null)
         {
             yield return(foreignKey.DependentToPrincipal);
         }
     }
     else
     {
         if (foreignKey.PrincipalToDependent != null)
         {
             yield return(foreignKey.PrincipalToDependent);
         }
     }
 }
Example #13
0
 public static string GetPropertyBaseName([NotNull] IReadOnlyForeignKey foreignKey)
 => foreignKey.DependentToPrincipal?.Name
 ?? foreignKey.GetReferencingSkipNavigations().FirstOrDefault()?.Inverse?.Name
 ?? foreignKey.PrincipalEntityType.ShortName();
Example #14
0
 public static IDependentKeyValueFactory <TKey>?GetDependentKeyValueFactory <TKey>(
     [NotNull] this IReadOnlyForeignKey foreignKey)
 => (IDependentKeyValueFactory <TKey>?)foreignKey.AsForeignKey().DependentKeyValueFactory;
Example #15
0
 /// <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 static bool AreCompatible(
     this IReadOnlyForeignKey foreignKey,
     IReadOnlyForeignKey duplicateForeignKey,
     in StoreObjectIdentifier storeObject,
Example #16
0
 public static IReadOnlyNavigation?GetNavigation([NotNull] this IReadOnlyForeignKey foreignKey, bool pointsToPrincipal)
 => pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent;
Example #17
0
 /// <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 static bool IsSelfReferencing(this IReadOnlyForeignKey foreignKey)
 => foreignKey.DeclaringEntityType == foreignKey.PrincipalEntityType;
Example #18
0
    /// <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 virtual string GetDependentEndCandidateNavigationPropertyName(IReadOnlyForeignKey foreignKey)
    {
        var candidateName = FindCandidateNavigationName(foreignKey.Properties);

        return(!string.IsNullOrEmpty(candidateName) ? candidateName : foreignKey.PrincipalEntityType.ShortName());
    }
 public static ForeignKey AsForeignKey([NotNull] this IReadOnlyForeignKey foreignKey, [NotNull][CallerMemberName] string methodName = "")
 => MetadataExtensions.AsConcreteMetadataType <IReadOnlyForeignKey, ForeignKey>(foreignKey, methodName);
 /// <summary>
 ///     Returns the foreign key constraint name.
 /// </summary>
 /// <param name="foreignKey"> The foreign key. </param>
 /// <param name="storeObject"> The identifier of the containing store object. </param>
 /// <param name="principalStoreObject"> The identifier of the principal store object. </param>
 /// <returns> The foreign key constraint name. </returns>
 public static string?GetConstraintName(
     this IReadOnlyForeignKey foreignKey,
     in StoreObjectIdentifier storeObject,
 public static IDependentsMap CreateDependentsMapFactory([NotNull] this IReadOnlyForeignKey foreignKey)
 => foreignKey.AsForeignKey().DependentsMapFactory();