src/Shared/EntityFramework/Core/Mapping/Update/Internal/UpdateCommand.cs
// <summary>
// Determines model level dependencies for the current command. Dependencies are based
// on the model operations performed by the command (adding or deleting entities or relationships).
// </summary>
Determines model level dependencies for the current command. Dependencies are based
on the model operations performed by the command (adding or deleting entities or relationships).
Determines model level dependencies for the current command . Dependencies are based on the model operations performed by the command ( adding or deleting entities or relationships ) .
internal void GetRequiredAndProducedEntities(
            UpdateTranslator translator,
            KeyToListMap<EntityKey, UpdateCommand> addedEntities,
            KeyToListMap<EntityKey, UpdateCommand> deletedEntities,
            KeyToListMap<EntityKey, UpdateCommand> addedRelationships,
            KeyToListMap<EntityKey, UpdateCommand> deletedRelationships)
        {
            var stateEntries = GetStateEntries(translator);
            foreach (var stateEntry in stateEntries)
            {
                if (!stateEntry.IsRelationship)
                {
                    if (stateEntry.State
                        == EntityState.Added)
                    {
                        addedEntities.Add(stateEntry.EntityKey, this);
                    }
                    else if (stateEntry.State
                             == EntityState.Deleted)
                    {
                        deletedEntities.Add(stateEntry.EntityKey, this);
                    }
                }
            }
            if (null != OriginalValues)
            {
                AddReferencedEntities(translator, OriginalValues, deletedRelationships);
            }
            if (null != CurrentValues)
            {
                AddReferencedEntities(translator, CurrentValues, addedRelationships);
            }
            foreach (var stateEntry in stateEntries)
            {
                if (stateEntry.IsRelationship)
                {
                    var isAdded = stateEntry.State == EntityState.Added;
                    if (isAdded || stateEntry.State == EntityState.Deleted)
                    {
                        var record = isAdded ? stateEntry.CurrentValues : stateEntry.OriginalValues;
                        Debug.Assert(2 == record.FieldCount, "non-binary relationship?");
                        var end1 = (EntityKey)record[0];
                        var end2 = (EntityKey)record[1];
                        var affected = isAdded ? addedRelationships : deletedRelationships;
                        affected.Add(end1, this);
                        affected.Add(end2, this);
                    }
                }
            }
        }
internal void GetRequiredAndProducedEntities ( UpdateTranslator translator , KeyToListMap < EntityKey , UpdateCommand > addedEntities , KeyToListMap < EntityKey , UpdateCommand > deletedEntities , KeyToListMap < EntityKey , UpdateCommand > addedRelationships , KeyToListMap < EntityKey , UpdateCommand > deletedRelationships ) { var stateEntries = GetStateEntries ( translator ) ; foreach ( var stateEntry in stateEntries ) { if ( ! stateEntry . IsRelationship ) { if ( stateEntry . State == EntityState . Added ) { addedEntities . Add ( stateEntry . EntityKey , this ) ; } else if ( stateEntry . State == EntityState . Deleted ) { deletedEntities . Add ( stateEntry . EntityKey , this ) ; } } } if ( null != OriginalValues ) { AddReferencedEntities ( translator , OriginalValues , deletedRelationships ) ; } if ( null != CurrentValues ) { AddReferencedEntities ( translator , CurrentValues , addedRelationships ) ; } foreach ( var stateEntry in stateEntries ) { if ( stateEntry . IsRelationship ) { var isAdded = stateEntry . State == EntityState . Added ; if ( isAdded || stateEntry . State == EntityState . Deleted ) { var record = isAdded ? stateEntry . CurrentValues : stateEntry . OriginalValues ; Debug . Assert ( 2 == record . FieldCount , " " ) ; var end1 = ( EntityKey ) record [ 0 ] ; var end2 = ( EntityKey ) record [ 1 ] ; var affected = isAdded ? addedRelationships : deletedRelationships ; affected . Add ( end1 , this ) ; affected . Add ( end2 , this ) ; } } } }
Determines model level dependencies for the current command.
Determines model level dependencies for the current command .
// process foreign keys // if a foreign key being deleted, it 'frees' or 'produces' the referenced key // if a foreign key is being added, if requires the referenced key // process relationships // only worry about the relationship if it is being added or deleted // relationships require the entity when they're added and free the entity when they're deleted... // both ends are being modified by the relationship
translator
UpdateTranslator
addedEntities
KeyToListMap<EntityKey, UpdateCommand>
deletedEntities
KeyToListMap<EntityKey, UpdateCommand>
addedRelationships
KeyToListMap<EntityKey, UpdateCommand>
deletedRelationships
KeyToListMap<EntityKey, UpdateCommand>