src/Shared/EntityFramework/Core/Objects/DataClasses/EntityReference.cs
// <summary>
// Takes key values from the given principal key and transfers them to the foreign key properties
// of the dependant entry.  This method requires a context, but does not require that either
// entity or key is in the context.  This allows it to work in NoTracking cases where we have the context
// but we're not tracked by that context.
// </summary>
// <param name="dependentEntity"> The entity into which foreign key values will be written </param>
// <param name="principalKey"> The key from which key values will be obtained </param>
Takes key values from the given principal key and transfers them to the foreign key properties
of the dependant entry.  This method requires a context, but does not require that either
entity or key is in the context.  This allows it to work in NoTracking cases where we have the context
but we're not tracked by that context.
Takes key values from the given principal key and transfers them to the foreign key properties of the dependant entry . This method requires a context but does not require that either entity or key is in the context . This allows it to work in NoTracking cases where we have the context but we ' re not tracked by that context .
internal void UpdateForeignKeyValues(IEntityWrapper dependentEntity, EntityKey principalKey)
        {
            DebugCheck.NotNull(dependentEntity.Entity);
            DebugCheck.NotNull(principalKey);
            Debug.Assert(!principalKey.IsTemporary, "Cannot update from a temp key");
            Debug.Assert(IsForeignKey, "cannot update foreign key values if the relationship is not a FK");
            var constraint = ((AssociationType)RelationMetadata).ReferentialConstraints[0];
            Debug.Assert(constraint != null, "null constraint");
            var stateManager = ObjectContext.ObjectStateManager;
            stateManager.TransactionManager.BeginForeignKeyUpdate(this);
            try
            {
                var dependentEntitySet = ((AssociationSet)RelationshipSet).AssociationSetEnds[FromEndMember.Name].EntitySet;
                var dependentTypeMetadata = stateManager.GetOrAddStateManagerTypeMetadata(dependentEntity.IdentityType, dependentEntitySet);
                for (var i = 0; i < constraint.FromProperties.Count; i++)
                {
                    var value = principalKey.FindValueByName(constraint.FromProperties[i].Name);
                    var dependentOrdinal = dependentTypeMetadata.GetOrdinalforOLayerMemberName(constraint.ToProperties[i].Name);
                    var currentValue = dependentTypeMetadata.Member(dependentOrdinal).GetValue(dependentEntity.Entity);
                    if (!ByValueEqualityComparer.Default.Equals(currentValue, value))
                    {
                        dependentEntity.SetCurrentValue(
                            dependentEntity.ObjectStateEntry,
                            dependentTypeMetadata.Member(dependentOrdinal),
                            -1,
                            dependentEntity.Entity,
                            value);
                    }
                }
                SetCachedForeignKey(principalKey, dependentEntity.ObjectStateEntry);
                if (WrappedOwner.ObjectStateEntry != null)
                {
                    stateManager.ForgetEntryWithConceptualNull(WrappedOwner.ObjectStateEntry, resetAllKeys: false);
                }
            }
            finally
            {
                stateManager.TransactionManager.EndForeignKeyUpdate();
            }
        }
internal void UpdateForeignKeyValues ( IEntityWrapper dependentEntity , EntityKey principalKey ) { DebugCheck . NotNull ( dependentEntity . Entity ) ; DebugCheck . NotNull ( principalKey ) ; Debug . Assert ( ! principalKey . IsTemporary , " " ) ; Debug . Assert ( IsForeignKey , " " ) ; var constraint = ( ( AssociationType ) RelationMetadata ) . ReferentialConstraints [ 0 ] ; Debug . Assert ( constraint != null , " " ) ; var stateManager = ObjectContext . ObjectStateManager ; stateManager . TransactionManager . BeginForeignKeyUpdate ( this ) ; try { var dependentEntitySet = ( ( AssociationSet ) RelationshipSet ) . AssociationSetEnds [ FromEndMember . Name ] . EntitySet ; var dependentTypeMetadata = stateManager . GetOrAddStateManagerTypeMetadata ( dependentEntity . IdentityType , dependentEntitySet ) ; for ( var i = 0 ; i < constraint . FromProperties . Count ; i ++ ) { var value = principalKey . FindValueByName ( constraint . FromProperties [ i ] . Name ) ; var dependentOrdinal = dependentTypeMetadata . GetOrdinalforOLayerMemberName ( constraint . ToProperties [ i ] . Name ) ; var currentValue = dependentTypeMetadata . Member ( dependentOrdinal ) . GetValue ( dependentEntity . Entity ) ; if ( ! ByValueEqualityComparer . Default . Equals ( currentValue , value ) ) { dependentEntity . SetCurrentValue ( dependentEntity . ObjectStateEntry , dependentTypeMetadata . Member ( dependentOrdinal ) , - 1 , dependentEntity . Entity , value ) ; } } SetCachedForeignKey ( principalKey , dependentEntity . ObjectStateEntry ) ; if ( WrappedOwner . ObjectStateEntry != null ) { stateManager . ForgetEntryWithConceptualNull ( WrappedOwner . ObjectStateEntry , resetAllKeys : false ) ; } } finally { stateManager . TransactionManager . EndForeignKeyUpdate ( ) ; } }
Takes key values from the given principal key and transfers them to the foreign key properties
of the dependant entry.
Takes key values from the given principal key and transfers them to the foreign key properties of the dependant entry .
dependentEntity
IEntityWrapper
The entity into which foreign key values will be written
The entity into which foreign key values will be written
principalKey
EntityKey
The key from which key values will be obtained
The key from which key values will be obtained