コード例 #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?.JoinEntityType != null &&
                Inverse.JoinEntityType != JoinEntityType)
            {
                throw new InvalidOperationException(
                          CoreStrings.SkipInverseMismatchedForeignKey(
                              foreignKey.Properties.Format(),
                              Name, JoinEntityType.DisplayName(),
                              Inverse.Name, Inverse.JoinEntityType.DisplayName()));
            }

            return(isChanging
                ? (ForeignKey)DeclaringEntityType.Model.ConventionDispatcher
                   .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey)
                : foreignKey);
        }
コード例 #2
0
ファイル: SkipNavigation.cs プロジェクト: SwishBoy/efcore
        /// <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.JoinEntityType != null &&
                JoinEntityType != null &&
                inverse.JoinEntityType != JoinEntityType)
            {
                throw new InvalidOperationException(CoreStrings.SkipInverseMismatchedJoinType(
                                                        inverse.Name, inverse.JoinEntityType.DisplayName(), Name, JoinEntityType.DisplayName()));
            }

            Inverse = inverse;
            UpdateInverseConfigurationSource(configurationSource);

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