/// <summary>
        ///     Called after a navigation property that has an attribute is added to an entity type.
        /// </summary>
        /// <param name="relationshipBuilder"> The builder for the relationship. </param>
        /// <param name="navigation"> The navigation. </param>
        /// <param name="attribute"> The attribute. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public override void ProcessNavigationAdded(
            IConventionRelationshipBuilder relationshipBuilder,
            IConventionNavigation navigation,
            InversePropertyAttribute attribute,
            IConventionContext <IConventionNavigation> context)
        {
            if (relationshipBuilder.Metadata.DeclaringEntityType.HasDefiningNavigation() ||
                relationshipBuilder.Metadata.DeclaringEntityType.IsOwned() ||
                relationshipBuilder.Metadata.PrincipalEntityType.HasDefiningNavigation() ||
                relationshipBuilder.Metadata.PrincipalEntityType.IsOwned())
            {
                return;
            }

            var newRelationship = ConfigureInverseNavigation(
                navigation.DeclaringEntityType.Builder,
                navigation.GetIdentifyingMemberInfo(),
                navigation.GetTargetType().Builder,
                attribute);

            if (newRelationship != relationshipBuilder)
            {
                if (newRelationship == null)
                {
                    context.StopProcessingIfChanged(null);
                    return;
                }

                var newNavigation = navigation.IsDependentToPrincipal()
                    ? newRelationship.Metadata.DependentToPrincipal
                    : newRelationship.Metadata.PrincipalToDependent;

                context.StopProcessingIfChanged(newNavigation);
            }
        }
コード例 #2
0
        /// <summary>
        ///     Returns the attributes applied to the given navigation.
        /// </summary>
        /// <param name="entityType"> The entity type. </param>
        /// <param name="navigation"> The navigation. </param>
        /// <typeparam name="TCustomAttribute"> The attribute type to look for. </typeparam>
        /// <returns> The attributes applied to the given navigation. </returns>
        protected static IEnumerable <TCustomAttribute> GetAttributes <TCustomAttribute>(
            [NotNull] IConventionEntityType entityType, [NotNull] IConventionNavigation navigation)
            where TCustomAttribute : Attribute
        {
            var memberInfo = navigation.GetIdentifyingMemberInfo();

            if (!entityType.HasClrType() ||
                memberInfo == null)
            {
                return(Enumerable.Empty <TCustomAttribute>());
            }

            return(Attribute.IsDefined(memberInfo, typeof(TCustomAttribute), inherit: true)
                ? memberInfo.GetCustomAttributes <TCustomAttribute>(true)
                : Enumerable.Empty <TCustomAttribute>());
        }
コード例 #3
0
 private static InversePropertyAttribute?GetInversePropertyAttribute(IConventionNavigation navigation)
 => GetAttribute <InversePropertyAttribute>(navigation.GetIdentifyingMemberInfo());
コード例 #4
0
 private static ForeignKeyAttribute GetForeignKeyAttribute(IConventionNavigation navigation)
 => GetAttribute <ForeignKeyAttribute>(navigation.GetIdentifyingMemberInfo());
コード例 #5
0
 /// <summary>
 ///     Returns the attributes applied to the given navigation.
 /// </summary>
 /// <param name="entityType"> The entity type. </param>
 /// <param name="navigation"> The navigation. </param>
 /// <typeparam name="TCustomAttribute"> The attribute type to look for. </typeparam>
 /// <returns> The attributes applied to the given navigation. </returns>
 protected static IEnumerable<TCustomAttribute> GetAttributes<TCustomAttribute>(
     [NotNull] IConventionEntityType entityType, [NotNull] IConventionNavigation navigation)
     where TCustomAttribute : Attribute
     => GetAttributes<TCustomAttribute>(entityType, navigation.GetIdentifyingMemberInfo());