Example #1
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 ForeignKey SetForeignKey([CanBeNull] ForeignKey foreignKey, ConfigurationSource configurationSource)
        {
            var oldForeignKey = ForeignKey;
            var isChanging    = foreignKey != ForeignKey;

            if (oldForeignKey != null)
            {
                oldForeignKey.ReferencingSkipNavigations.Remove(this);
            }

            if (foreignKey == null)
            {
                ForeignKey = null;
                _foreignKeyConfigurationSource = null;

                return(isChanging
                    ? (ForeignKey)DeclaringEntityType.Model.ConventionDispatcher
                       .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey)
                    : foreignKey);
            }

            var expectedEntityType = IsOnDependent ? foreignKey.DeclaringEntityType : foreignKey.PrincipalEntityType;

            if (expectedEntityType != DeclaringEntityType)
            {
                var message = IsOnDependent
                    ? CoreStrings.SkipNavigationForeignKeyWrongDependentType(
                    foreignKey.Properties.Format(), Name, DeclaringEntityType.DisplayName(), expectedEntityType.DisplayName())
                    : CoreStrings.SkipNavigationForeignKeyWrongPrincipalType(
                    foreignKey.Properties.Format(), Name, DeclaringEntityType.DisplayName(), expectedEntityType.DisplayName());
                throw new InvalidOperationException(message);
            }

            ProcessForeignKey(foreignKey);
            UpdateForeignKeyConfigurationSource(configurationSource);

            if (Inverse?.AssociationEntityType != null &&
                Inverse.AssociationEntityType != AssociationEntityType)
            {
                throw new InvalidOperationException(CoreStrings.SkipInverseMismatchedForeignKey(
                                                        foreignKey.Properties.Format(),
                                                        Name, AssociationEntityType.DisplayName(),
                                                        Inverse.Name, Inverse.AssociationEntityType.DisplayName()));
            }

            return(isChanging
                ? (ForeignKey)DeclaringEntityType.Model.ConventionDispatcher
                   .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey)
                : foreignKey);
        }
Example #2
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 SkipNavigation SetInverse([CanBeNull] SkipNavigation inverse, ConfigurationSource configurationSource)
        {
            var oldInverse = Inverse;
            var isChanging = inverse != Inverse;

            if (inverse == null)
            {
                Inverse = null;
                _inverseConfigurationSource = null;

                return(isChanging
                    ? (SkipNavigation)DeclaringEntityType.Model.ConventionDispatcher
                       .OnSkipNavigationInverseChanged(Builder, inverse, oldInverse)
                    : inverse);
            }

            if (inverse.DeclaringEntityType != TargetEntityType)
            {
                throw new InvalidOperationException(CoreStrings.SkipNavigationWrongInverse(
                                                        inverse.Name, inverse.DeclaringEntityType.DisplayName(), Name, TargetEntityType.DisplayName()));
            }

            if (inverse.AssociationEntityType != null &&
                AssociationEntityType != null &&
                inverse.AssociationEntityType != AssociationEntityType)
            {
                throw new InvalidOperationException(CoreStrings.SkipInverseMismatchedAssociationType(
                                                        inverse.Name, inverse.AssociationEntityType.DisplayName(), Name, AssociationEntityType.DisplayName()));
            }

            Inverse = inverse;
            UpdateInverseConfigurationSource(configurationSource);

            return(isChanging
                ? (SkipNavigation)DeclaringEntityType.Model.ConventionDispatcher
                   .OnSkipNavigationInverseChanged(Builder, inverse, oldInverse)
                : inverse);
        }