コード例 #1
0
        internal void ValidateSettingRIConstraints(IEntityWrapper targetEntity, bool settingToNull, bool changingForeignKeyValue)
        {
            bool isNoTracking = targetEntity != null && targetEntity.MergeOption == MergeOption.NoTracking;

            if (settingToNull ||                    // setting the principle to null
                changingForeignKeyValue ||          // existing key does not match incoming key
                (targetEntity != null &&
                 !isNoTracking &&
                 (targetEntity.ObjectStateEntry == null ||                                                    // setting to a detached principle
                  (EntityKey == null && targetEntity.ObjectStateEntry.State == EntityState.Deleted ||         // setting to a deleted principle
                   (CachedForeignKey == null && targetEntity.ObjectStateEntry.State == EntityState.Added))))) // setting to an added principle
            {
                throw EntityUtil.CannotChangeReferentialConstraintProperty();
            }
        }
コード例 #2
0
        /// <summary>
        /// This method is called when either the EntityKey or the Value property is set to null when it is
        /// already null. For an FK association of a tracked entity the method will attempt to null FKs
        /// thereby deleting the relationship. This may result in conceptual nulls being set.
        /// </summary>
        internal bool AttemptToNullFKsOnRefOrKeySetToNull()
        {
            if (ReferenceValue.Entity == null &&
                WrappedOwner.Entity != null &&
                WrappedOwner.Context != null &&
                !UsingNoTracking &&
                IsForeignKey)
            {
                // For identifying relationships, we throw, since we cannot set primary key values to null, unless
                // the entity is in the Added state.
                if (WrappedOwner.ObjectStateEntry.State != EntityState.Added &&
                    IsDependentEndOfReferentialConstraint(checkIdentifying: true))
                {
                    throw EntityUtil.CannotChangeReferentialConstraintProperty();
                }

                // For unloaded FK relationships in the context we attempt to null FK values here, which will
                // delete the relationship.
                RemoveFromLocalCache(EntityWrapperFactory.NullWrapper, resetIsLoaded: true, preserveForeignKey: false);

                return(true);
            }
            return(false);
        }