1 //---------------------------------------------------------------------
2 // <copyright file="EntityReference.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
8 //---------------------------------------------------------------------
10 namespace System.Data.Objects.DataClasses
12 using System.Collections.Generic;
14 using System.Data.Common.Utils;
15 using System.Data.Metadata.Edm;
16 using System.Data.Objects.Internal;
17 using System.Diagnostics;
19 using System.Runtime.Serialization;
22 /// Models a relationship end with multiplicity 1.
26 public abstract class EntityReference : RelatedEnd
32 // The following fields are serialized. Adding or removing a serialized field is considered
33 // a breaking change. This includes changing the field type or field name of existing
34 // serialized fields. If you need to make this kind of change, it may be possible, but it
35 // will require some custom serialization/deserialization code.
37 // The following field is valid only for detached EntityReferences, see EntityKey property for more details.
38 private EntityKey _detachedEntityKey = null;
40 // The following field is used to cache the FK value to the principal for FK relationships.
41 // It is okay to not serialize this field because it is only used when the entity is tracked.
42 // For a detached entity it can always be null and cause no problems.
44 private EntityKey _cachedForeignKey;
51 /// The default constructor is required for some serialization scenarios. It should not be used to
52 /// create new EntityReferences. Use the GetRelatedReference or GetRelatedEnd methods on the RelationshipManager
55 internal EntityReference()
59 internal EntityReference(IEntityWrapper wrappedOwner, RelationshipNavigation navigation, IRelationshipFixer relationshipFixer)
60 : base(wrappedOwner, navigation, relationshipFixer)
65 /// Returns the EntityKey of the target entity associated with this EntityReference.
67 /// Is non-null in the following scenarios:
68 /// (a) Entities are tracked by a context and an Unchanged or Added client-side relationships exists for this EntityReference's owner with the
69 /// same RelationshipName and source role. This relationship could have been created explicitly by the user (e.g. by setting
70 /// the EntityReference.Value, setting this property directly, or by calling EntityCollection.Add) or automatically through span queries.
71 /// (b) If the EntityKey was non-null before detaching an entity from the context, it will still be non-null after detaching, until any operation
72 /// occurs that would set it to null, as described below.
73 /// (c) Entities are detached and the EntityKey is explicitly set to non-null by the user.
74 /// (d) Entity graph was created using a NoTracking query with full span
76 /// Is null in the following scenarios:
77 /// (a) Entities are tracked by a context but there is no Unchanged or Added client-side relationship for this EntityReference's owner with the
78 /// same RelationshipName and source role.
79 /// (b) Entities are tracked by a context and a relationship exists, but the target entity has a temporary key (i.e. it is Added) or the key
80 /// is one of the special keys
81 /// (c) Entities are detached and the relationship was explicitly created by the user.
84 public EntityKey EntityKey
86 // This is the only scenario where it is valid to have a null Owner, so don't check it
90 if (this.ObjectContext != null && !UsingNoTracking)
92 Debug.Assert(this.WrappedOwner.Entity != null, "Unexpected null Owner on EntityReference attached to a context");
94 EntityKey attachedKey = null;
96 // If this EntityReference contains an entity, look up the key on that object
97 if (CachedValue.Entity != null)
99 // While processing an attach the owner may have a context while the target does not. This means
100 // that the target may gave an entity but not yet have an attached entity key.
101 attachedKey = CachedValue.EntityKey;
102 if (attachedKey != null && !IsValidEntityKeyType(attachedKey))
104 // don't return temporary or special keys from this property
112 // For dependent ends, return the value of the cached foreign key if it is not conceptually null
113 if (IsDependentEndOfReferentialConstraint(false) && _cachedForeignKey != null)
115 if (!ForeignKeyFactory.IsConceptualNullKey(_cachedForeignKey))
117 attachedKey = _cachedForeignKey;
122 // Principal ends or ends that haven't been fixed up yet (i.e during Add/Attach) should use the DetachedEntityKey value
123 // that contains the last known value that was set
124 attachedKey = DetachedEntityKey;
129 // There could still be an Added or Unchanged relationship with a stub entry
130 EntityKey ownerKey = WrappedOwner.EntityKey;
131 foreach (RelationshipEntry relationshipEntry in this.ObjectContext.ObjectStateManager.FindRelationshipsByKey(ownerKey))
133 // We only care about the relationships that match the AssociationSet and source role for the owner of this EntityReference
134 if (relationshipEntry.State != EntityState.Deleted &&
135 relationshipEntry.IsSameAssociationSetAndRole((AssociationSet)RelationshipSet, (AssociationEndMember)this.FromEndProperty, ownerKey))
137 Debug.Assert(attachedKey == null, "Found more than one non-Deleted relationship for the same AssociationSet and source role");
138 attachedKey = relationshipEntry.RelationshipWrapper.GetOtherEntityKey(ownerKey);
139 // key should never be temporary or special since it came from a key entry
144 Debug.Assert(attachedKey == null || IsValidEntityKeyType(attachedKey),
145 "Unexpected temporary or special key");
150 return DetachedEntityKey;
155 SetEntityKey(value, forceFixup: false);
159 internal void SetEntityKey(EntityKey value, bool forceFixup)
161 if (value != null && value == EntityKey && (ReferenceValue.Entity != null || (ReferenceValue.Entity == null && !forceFixup)))
163 // "no-op" -- this is not really no-op in the attached case, because at a minimum we have to do a key lookup,
164 // worst case we have to review all relationships for the owner entity
165 // However, if we don't do this, we can get into a scenario where we are setting the key to the same thing it's already set to
166 // and this could have side effects, especially with RI constraints and cascade delete. We don't want to delete something
167 // and then add it back, if that deleting could have additional unexpected effects. Don't bother doing this check if value is
168 // null, because EntityKey could be null even if there are Added/Unchanged relationships, if the target entity has a temporary key.
169 // In that case, we still need to delete that existing relationship, so it's not a no-op
173 if (this.ObjectContext != null && !UsingNoTracking)
175 Debug.Assert(this.WrappedOwner.Entity != null, "Unexpected null Owner on EntityReference attached to a context");
177 // null is a valid value for the EntityKey, but temporary and special keys are not
178 // devnote: Can't check this on detached references because this property could be set to a temp key during deserialization,
179 // if the key hasn't finished deserializing yet.
180 if (value != null && !IsValidEntityKeyType(value))
182 throw EntityUtil.CannotSetSpecialKeys();
187 if (AttemptToNullFKsOnRefOrKeySetToNull())
189 DetachedEntityKey = null;
193 ReferenceValue = EntityWrapperFactory.NullWrapper;
198 // Verify that the key has the right EntitySet for this RelationshipSet
199 EntitySet targetEntitySet = value.GetEntitySet(ObjectContext.MetadataWorkspace);
200 CheckRelationEntitySet(targetEntitySet);
201 value.ValidateEntityKey(ObjectContext.MetadataWorkspace, targetEntitySet, true /*isArgumentException */, "value");
203 ObjectStateManager manager = this.ObjectContext.ObjectStateManager;
205 // If we already have an entry with this key, we just need to create a relationship with it
206 bool addNewRelationship = false;
207 // If we don't already have any matching entries for this key, we'll have to create a new entry
208 bool addKeyEntry = false;
209 EntityEntry targetEntry = manager.FindEntityEntry(value);
210 if (targetEntry != null)
212 // If it's not a key entry, just use the entity to set this reference's Value
213 if (!targetEntry.IsKeyEntry)
215 // Delegate to the Value property to clear any existing relationship
216 // and to add the new one. This will fire the appropriate events and
217 // ensure that the related ends are connected.
219 // It has to be a TEntity since we already verified that the EntitySet is correct above
220 this.ReferenceValue = targetEntry.WrappedEntity;
224 // if the existing entry is a key entry, we just need to
225 // add a new relationship between the source entity and that key
226 addNewRelationship = true;
231 // no entry exists, so we'll need to add a key along with the relationship
232 addKeyEntry = !IsForeignKey;
233 addNewRelationship = true;
236 if (addNewRelationship)
238 EntityKey ownerKey = ValidateOwnerWithRIConstraints(targetEntry == null ? null : targetEntry.WrappedEntity, value, checkBothEnds: true);
240 // Verify that the owner is in a valid state for adding a relationship
241 ValidateStateForAdd(this.WrappedOwner);
245 manager.AddKeyEntry(value, targetEntitySet);
248 // First, clear any existing relationships
249 manager.TransactionManager.EntityBeingReparented = WrappedOwner.Entity;
252 ClearCollectionOrRef(null, null, /*doCascadeDelete*/ false);
256 manager.TransactionManager.EntityBeingReparented = null;
259 // Then add the new one
262 DetachedEntityKey = value;
263 // Update the FK values in this entity
264 if (IsDependentEndOfReferentialConstraint(false))
266 UpdateForeignKeyValues(WrappedOwner, value);
271 RelationshipWrapper wrapper = new RelationshipWrapper((AssociationSet)RelationshipSet, RelationshipNavigation.From, ownerKey, RelationshipNavigation.To, value);
272 // Add the relationship in the unchanged state if
273 EntityState relationshipState = EntityState.Added;
275 // If this is an unchanged/modified dependent end of a relationship and we are allowing the EntityKey to be set
276 // create the relationship in the Unchanged state because the state must "match" the dependent end state
277 if (!ownerKey.IsTemporary && IsDependentEndOfReferentialConstraint(false))
279 relationshipState = EntityState.Unchanged;
281 manager.AddNewRelation(wrapper, relationshipState);
288 // Just set the field for detached object -- during Attach/Add we will make sure this value
289 // is not in conflict if the EntityReference contains a real entity. We cannot always determine the
290 // EntityKey for any real entity in the detached state, so we don't bother to do it here.
291 DetachedEntityKey = value;
296 /// This method is called when either the EntityKey or the Value property is set to null when it is
297 /// already null. For an FK association of a tracked entity the method will attempt to null FKs
298 /// thereby deleting the relationship. This may result in conceptual nulls being set.
300 internal bool AttemptToNullFKsOnRefOrKeySetToNull()
302 if (ReferenceValue.Entity == null &&
303 WrappedOwner.Entity != null &&
304 WrappedOwner.Context != null &&
308 // For identifying relationships, we throw, since we cannot set primary key values to null, unless
309 // the entity is in the Added state.
310 if (WrappedOwner.ObjectStateEntry.State != EntityState.Added &&
311 IsDependentEndOfReferentialConstraint(checkIdentifying: true))
313 throw EntityUtil.CannotChangeReferentialConstraintProperty();
316 // For unloaded FK relationships in the context we attempt to null FK values here, which will
317 // delete the relationship.
318 RemoveFromLocalCache(EntityWrapperFactory.NullWrapper, resetIsLoaded: true, preserveForeignKey: false);
325 internal EntityKey AttachedEntityKey
329 Debug.Assert(this.ObjectContext != null && !UsingNoTracking, "Should only need to access AttachedEntityKey property on attached EntityReferences");
330 return this.EntityKey;
334 internal EntityKey DetachedEntityKey
338 return _detachedEntityKey;
342 _detachedEntityKey = value;
346 internal EntityKey CachedForeignKey
350 return EntityKey ?? _cachedForeignKey;
354 internal void SetCachedForeignKey(EntityKey newForeignKey, EntityEntry source)
356 if (this.ObjectContext != null && this.ObjectContext.ObjectStateManager != null && // are we attached?
357 source != null && // do we have an entry?
358 _cachedForeignKey != null && !ForeignKeyFactory.IsConceptualNullKey(_cachedForeignKey) // do we have an fk?
359 && _cachedForeignKey != newForeignKey) // is the FK different from the one that we already have?
361 this.ObjectContext.ObjectStateManager.RemoveEntryFromForeignKeyIndex(_cachedForeignKey, source);
363 _cachedForeignKey = newForeignKey;
366 internal IEnumerable<EntityKey> GetAllKeyValues()
368 if (EntityKey != null)
370 yield return EntityKey;
373 if (_cachedForeignKey != null)
375 yield return _cachedForeignKey;
378 if (_detachedEntityKey != null)
380 yield return _detachedEntityKey;
384 internal abstract IEntityWrapper CachedValue
389 internal abstract IEntityWrapper ReferenceValue
396 internal EntityKey ValidateOwnerWithRIConstraints(IEntityWrapper targetEntity, EntityKey targetEntityKey, bool checkBothEnds)
398 EntityKey ownerKey = WrappedOwner.EntityKey;
400 // Check if Referential Constraints are violated
401 if ((object)ownerKey != null &&
402 !ownerKey.IsTemporary &&
403 IsDependentEndOfReferentialConstraint(checkIdentifying: true))
405 Debug.Assert(CachedForeignKey != null || EntityKey == null, "CachedForeignKey should not be null if EntityKey is not null.");
406 ValidateSettingRIConstraints(targetEntity,
407 targetEntityKey == null,
408 (this.CachedForeignKey != null && this.CachedForeignKey != targetEntityKey));
410 else if (checkBothEnds && targetEntity != null && targetEntity.Entity != null)
412 EntityReference otherEnd = GetOtherEndOfRelationship(targetEntity) as EntityReference;
413 if (otherEnd != null)
415 otherEnd.ValidateOwnerWithRIConstraints(WrappedOwner, ownerKey, checkBothEnds: false);
423 internal void ValidateSettingRIConstraints(IEntityWrapper targetEntity, bool settingToNull, bool changingForeignKeyValue)
425 bool isNoTracking = targetEntity != null && targetEntity.MergeOption == MergeOption.NoTracking;
427 if (settingToNull || // setting the principle to null
428 changingForeignKeyValue || // existing key does not match incoming key
429 (targetEntity != null &&
431 (targetEntity.ObjectStateEntry == null || // setting to a detached principle
432 (EntityKey == null && targetEntity.ObjectStateEntry.State == EntityState.Deleted || // setting to a deleted principle
433 (CachedForeignKey == null && targetEntity.ObjectStateEntry.State == EntityState.Added))))) // setting to an added principle
435 throw EntityUtil.CannotChangeReferentialConstraintProperty();
440 /// EntityReferences can only deferred load if they are empty
442 internal override bool CanDeferredLoad
451 /// Takes key values from the given principal entity and transfers them to the foreign key properties
452 /// of the dependant entry. This method requires a context, but does not require that either
453 /// entity is in the context. This allows it to work in NoTracking cases where we have the context
454 /// but we're not tracked by that context.
456 /// <param name="dependentEntity">The entity into which foreign key values will be written</param>
457 /// <param name="principalEntity">The entity from which key values will be obtained</param>
458 /// <param name="changedFKs">If non-null, then keeps track of FKs that have already been set such that an exception can be thrown if we find conflicting values</param>
459 /// <param name="forceChange">If true, then the property setter is called even if FK values already match,
460 /// which causes the FK properties to be marked as modified.</param>
461 internal void UpdateForeignKeyValues(IEntityWrapper dependentEntity, IEntityWrapper principalEntity, Dictionary<int, object> changedFKs, bool forceChange)
463 Debug.Assert(dependentEntity.Entity != null, "dependentEntity.Entity == null");
464 Debug.Assert(principalEntity.Entity != null, "principalEntity.Entity == null");
465 Debug.Assert(this.IsForeignKey, "cannot update foreign key values if the relationship is not a FK");
466 ReferentialConstraint constraint = ((AssociationType)this.RelationMetadata).ReferentialConstraints[0];
467 Debug.Assert(constraint != null, "null constraint");
469 bool isUnchangedDependent = (object)WrappedOwner.EntityKey != null &&
470 !WrappedOwner.EntityKey.IsTemporary &&
471 IsDependentEndOfReferentialConstraint(checkIdentifying: true);
473 ObjectStateManager stateManager = ObjectContext.ObjectStateManager;
474 stateManager.TransactionManager.BeginForeignKeyUpdate(this);
477 EntitySet principalEntitySet = ((AssociationSet)RelationshipSet).AssociationSetEnds[ToEndMember.Name].EntitySet;
478 StateManagerTypeMetadata principalTypeMetadata = stateManager.GetOrAddStateManagerTypeMetadata(principalEntity.IdentityType, principalEntitySet);
480 EntitySet dependentEntitySet = ((AssociationSet)RelationshipSet).AssociationSetEnds[FromEndProperty.Name].EntitySet;
481 StateManagerTypeMetadata dependentTypeMetadata = stateManager.GetOrAddStateManagerTypeMetadata(dependentEntity.IdentityType, dependentEntitySet);
483 var principalProps = constraint.FromProperties;
484 int numValues = principalProps.Count;
485 string[] keyNames = null;
486 object[] values = null;
489 keyNames = principalEntitySet.ElementType.KeyMemberNames;
490 values = new object[numValues];
492 for (int i = 0; i < numValues; i++)
494 int principalOrdinal = principalTypeMetadata.GetOrdinalforOLayerMemberName(principalProps[i].Name);
495 object value = principalTypeMetadata.Member(principalOrdinal).GetValue(principalEntity.Entity);
496 int dependentOrdinal = dependentTypeMetadata.GetOrdinalforOLayerMemberName(constraint.ToProperties[i].Name);
497 bool valueChanging = !ByValueEqualityComparer.Default.Equals(dependentTypeMetadata.Member(dependentOrdinal).GetValue(dependentEntity.Entity), value);
498 if (forceChange || valueChanging)
500 if (isUnchangedDependent)
502 ValidateSettingRIConstraints(principalEntity, settingToNull: value == null, changingForeignKeyValue: valueChanging);
504 // If we're tracking FK values that have already been set, then compare the value we are about to set
505 // to the value we previously set for this ordinal, if such a value exists. If they don't match then
506 // it means that we got conflicting FK values from two different PKs and we should throw.
507 if (changedFKs != null)
509 object previouslySetValue;
510 if (changedFKs.TryGetValue(dependentOrdinal, out previouslySetValue))
512 if (!ByValueEqualityComparer.Default.Equals(previouslySetValue, value))
514 throw new InvalidOperationException(System.Data.Entity.Strings.Update_ReferentialConstraintIntegrityViolation);
519 changedFKs[dependentOrdinal] = value;
522 dependentEntity.SetCurrentValue(
523 dependentEntity.ObjectStateEntry,
524 dependentTypeMetadata.Member(dependentOrdinal),
526 dependentEntity.Entity,
532 int keyIndex = Array.IndexOf(keyNames, principalProps[i].Name);
533 Debug.Assert(keyIndex >= 0 && keyIndex < numValues, "Could not find constraint prop name in entity set key names");
534 values[keyIndex] = value;
538 SetCachedForeignKey(new EntityKey(principalEntitySet, value), dependentEntity.ObjectStateEntry);
544 SetCachedForeignKey(new EntityKey(principalEntitySet, values), dependentEntity.ObjectStateEntry);
546 if (WrappedOwner.ObjectStateEntry != null)
548 stateManager.ForgetEntryWithConceptualNull(WrappedOwner.ObjectStateEntry, resetAllKeys: false);
553 stateManager.TransactionManager.EndForeignKeyUpdate();
558 /// Takes key values from the given principal key and transfers them to the foreign key properties
559 /// of the dependant entry. This method requires a context, but does not require that either
560 /// entity or key is in the context. This allows it to work in NoTracking cases where we have the context
561 /// but we're not tracked by that context.
563 /// <param name="dependentEntity">The entity into which foreign key values will be written</param>
564 /// <param name="principalEntity">The key from which key values will be obtained</param>
565 internal void UpdateForeignKeyValues(IEntityWrapper dependentEntity, EntityKey principalKey)
567 Debug.Assert(dependentEntity.Entity != null, "dependentEntity.Entity == null");
568 Debug.Assert(principalKey != null, "principalKey == null");
569 Debug.Assert(!principalKey.IsTemporary, "Cannot update from a temp key");
570 Debug.Assert(this.IsForeignKey, "cannot update foreign key values if the relationship is not a FK");
571 ReferentialConstraint constraint = ((AssociationType)this.RelationMetadata).ReferentialConstraints[0];
572 Debug.Assert(constraint != null, "null constraint");
574 ObjectStateManager stateManager = ObjectContext.ObjectStateManager;
575 stateManager.TransactionManager.BeginForeignKeyUpdate(this);
578 EntitySet dependentEntitySet = ((AssociationSet)RelationshipSet).AssociationSetEnds[FromEndProperty.Name].EntitySet;
579 StateManagerTypeMetadata dependentTypeMetadata = stateManager.GetOrAddStateManagerTypeMetadata(dependentEntity.IdentityType, dependentEntitySet);
581 for (int i = 0; i < constraint.FromProperties.Count; i++)
583 object value = principalKey.FindValueByName(constraint.FromProperties[i].Name);
584 int dependentOrdinal = dependentTypeMetadata.GetOrdinalforOLayerMemberName(constraint.ToProperties[i].Name);
585 object currentValue = dependentTypeMetadata.Member(dependentOrdinal).GetValue(dependentEntity.Entity);
586 if (!ByValueEqualityComparer.Default.Equals(currentValue, value))
588 dependentEntity.SetCurrentValue(
589 dependentEntity.ObjectStateEntry,
590 dependentTypeMetadata.Member(dependentOrdinal),
592 dependentEntity.Entity,
597 SetCachedForeignKey(principalKey, dependentEntity.ObjectStateEntry);
598 if (WrappedOwner.ObjectStateEntry != null)
600 stateManager.ForgetEntryWithConceptualNull(WrappedOwner.ObjectStateEntry, resetAllKeys: false);
605 stateManager.TransactionManager.EndForeignKeyUpdate();
609 internal object GetDependentEndOfReferentialConstraint(object relatedValue)
611 return IsDependentEndOfReferentialConstraint(checkIdentifying: false) ?
612 WrappedOwner.Entity :
616 internal bool NavigationPropertyIsNullOrMissing()
618 Debug.Assert(RelationshipNavigation != null, "null RelationshipNavigation");
620 return !TargetAccessor.HasProperty || WrappedOwner.GetNavigationPropertyValue(this) == null;
624 /// Attempts to null all FKs associated with the dependent end of this relationship on this entity.
625 /// This may result in setting conceptual nulls if the FK is not nullable.
627 internal void NullAllForeignKeys()
629 Debug.Assert(ObjectContext != null, "Nulling FKs only works when attached.");
630 Debug.Assert(IsForeignKey, "Cannot null FKs for independent associations.");
632 ObjectStateManager stateManager = ObjectContext.ObjectStateManager;
633 EntityEntry entry = WrappedOwner.ObjectStateEntry;
634 TransactionManager transManager = stateManager.TransactionManager;
635 if (!transManager.IsGraphUpdate && !transManager.IsAttachTracking && !transManager.IsRelatedEndAdd)
637 ReferentialConstraint constraint = ((AssociationType)RelationMetadata).ReferentialConstraints.Single();
638 if (TargetRoleName == constraint.FromRole.Name) // Only do this on the dependent end
640 if (transManager.IsDetaching)
642 // If the principal is being detached, then the dependent must be added back to the
643 // dangling keys index.
644 // Perf note: The dependent currently gets added when it is being detached and is then
645 // removed again later in the process. The code could be optimized to prevent this.
646 Debug.Assert(entry != null, "State entry must exist while detaching.");
647 EntityKey foreignKey = ForeignKeyFactory.CreateKeyFromForeignKeyValues(entry, this);
648 if (foreignKey != null)
650 stateManager.AddEntryContainingForeignKeyToIndex(foreignKey, entry);
653 else if (!ReferenceEquals(stateManager.EntityInvokingFKSetter, WrappedOwner.Entity) && !transManager.IsForeignKeyUpdate)
655 transManager.BeginForeignKeyUpdate(this);
658 bool unableToNull = true;
659 bool canSetModifiedProps = entry != null && (entry.State == EntityState.Modified || entry.State == EntityState.Unchanged);
660 EntitySet dependentEntitySet = ((AssociationSet)RelationshipSet).AssociationSetEnds[FromEndProperty.Name].EntitySet;
661 StateManagerTypeMetadata dependentTypeMetadata = stateManager.GetOrAddStateManagerTypeMetadata(WrappedOwner.IdentityType, dependentEntitySet);
663 for (int i = 0; i < constraint.FromProperties.Count; i++)
665 string propertyName = constraint.ToProperties[i].Name;
666 int dependentOrdinal = dependentTypeMetadata.GetOrdinalforOLayerMemberName(propertyName);
667 StateManagerMemberMetadata member = dependentTypeMetadata.Member(dependentOrdinal);
669 // This is a check for nullability in o-space. However, o-space nullability is not the
670 // same as nullability of the underlying type. In particular, one difference is that when
671 // attribute-based mapping is used then a property can be marked as not nullable in o-space
672 // even when the underlying CLR type is nullable. For such a case, we treat the property
673 // as if it were not nullable (since that's what we have shipped) even though we could
674 // technically set it to null.
675 if (member.ClrMetadata.Nullable)
677 // Only set the value to null if it is not already null.
678 if (member.GetValue(WrappedOwner.Entity) != null)
680 WrappedOwner.SetCurrentValue(
681 WrappedOwner.ObjectStateEntry,
682 dependentTypeMetadata.Member(dependentOrdinal),
689 // Given that the current value is null, this next check confirms that the original
690 // value is also null. If it isn't, then we must make sure that the entity is marked
692 // This case can happen because fixup in the entity can set the FK to null while processing
693 // a RelatedEnd operation. This will be detected by DetectChanges, but when performing
694 // RelatedEnd operations the user is not required to call DetectChanges.
695 if (canSetModifiedProps && WrappedOwner.ObjectStateEntry.OriginalValues.GetValue(dependentOrdinal) != null)
697 entry.SetModifiedProperty(propertyName);
700 unableToNull = false;
702 else if (canSetModifiedProps)
704 entry.SetModifiedProperty(propertyName);
709 // We were unable to null out the FK because all FK properties were non-nullable.
710 // We need to keep track of this state so that we treat the FK as null even though
711 // we were not able to null it. This prevents the FK from being used for fixup and
712 // also causes an exception to be thrown if an attempt is made to commit in this state.
714 //We should only set a conceptual null if the entity is tracked
717 //The CachedForeignKey may be null if we are putting
718 //back a Conceptual Null as part of roll back
719 EntityKey realKey = CachedForeignKey;
722 realKey = ForeignKeyFactory.CreateKeyFromForeignKeyValues(entry, this);
725 // Note that the realKey can still be null here for a situation where the key is marked not nullable
726 // in o-space and yet the underlying type is nullable and the entity has been added or attached with a null
727 // value for the property. This will cause SaveChanges to throw unless the entity is marked
728 // as deleted before SaveChanges is called, in which case we don't want to set a conceptual
729 // null here as the call might very well succeed in the database since, unless the FK is
730 // a concurrency token, the value we have for it is not used at all for the delete.
733 SetCachedForeignKey(ForeignKeyFactory.CreateConceptualNullKey(realKey), entry);
734 stateManager.RememberEntryWithConceptualNull(entry);
740 SetCachedForeignKey(null, entry);
745 transManager.EndForeignKeyUpdate();