src/Shared/EntityFramework/Core/Mapping/Update/Internal/KeyManager.cs
// <summary>
// Registers an added entity so that it can be matched by a foreign key lookup.
// </summary>
Registers an added entity so that it can be matched by a foreign key lookup.
Registers an added entity so that it can be matched by a foreign key lookup .
internal void RegisterKeyValueForAddedEntity(IEntityStateEntry addedEntry)
        {
            DebugCheck.NotNull(addedEntry);
            Debug.Assert(!addedEntry.IsRelationship);
            Debug.Assert(!addedEntry.IsKeyEntry);
            Debug.Assert(addedEntry.EntityKey.IsTemporary);
            var tempKey = addedEntry.EntityKey;
            EntityKey valueKey;
            var keyMembers = addedEntry.EntitySet.ElementType.KeyMembers;
            var currentValues = addedEntry.CurrentValues;
            var keyValues = new object[keyMembers.Count];
            var hasNullValue = false;
            for (int i = 0, n = keyMembers.Count; i < n; i++)
            {
                var ordinal = currentValues.GetOrdinal(keyMembers[i].Name);
                if (currentValues.IsDBNull(ordinal))
                {
                    hasNullValue = true;
                    break;
                }
                else
                {
                    keyValues[i] = currentValues.GetValue(ordinal);
                }
            }
            if (hasNullValue)
            {
                return;
            }
            else
            {
                valueKey = keyValues.Length == 1
                               ? new EntityKey(addedEntry.EntitySet, keyValues[0])
                               : new EntityKey(addedEntry.EntitySet, keyValues);
            }
            if (_valueKeyToTempKey.ContainsKey(valueKey))
            {
                _valueKeyToTempKey[valueKey] = null;
            }
            else
            {
                _valueKeyToTempKey.Add(valueKey, tempKey);
            }
        }
internal void RegisterKeyValueForAddedEntity ( IEntityStateEntry addedEntry ) { DebugCheck . NotNull ( addedEntry ) ; Debug . Assert ( ! addedEntry . IsRelationship ) ; Debug . Assert ( ! addedEntry . IsKeyEntry ) ; Debug . Assert ( addedEntry . EntityKey . IsTemporary ) ; var tempKey = addedEntry . EntityKey ; EntityKey valueKey ; var keyMembers = addedEntry . EntitySet . ElementType . KeyMembers ; var currentValues = addedEntry . CurrentValues ; var keyValues = new object [ keyMembers . Count ] ; var hasNullValue = false ; for ( int i = 0 , n = keyMembers . Count ; i < n ; i ++ ) { var ordinal = currentValues . GetOrdinal ( keyMembers [ i ] . Name ) ; if ( currentValues . IsDBNull ( ordinal ) ) { hasNullValue = true ; break ; } else { keyValues [ i ] = currentValues . GetValue ( ordinal ) ; } } if ( hasNullValue ) { return ; } else { valueKey = keyValues . Length == 1 ? new EntityKey ( addedEntry . EntitySet , keyValues [ 0 ] ) : new EntityKey ( addedEntry . EntitySet , keyValues ) ; } if ( _valueKeyToTempKey . ContainsKey ( valueKey ) ) { _valueKeyToTempKey [ valueKey ] = null ; } else { _valueKeyToTempKey . Add ( valueKey , tempKey ) ; } }
Registers an added entity so that it can be matched by a foreign key lookup.
Registers an added entity so that it can be matched by a foreign key lookup .
// map temp key to 'value' key (if all values of the key are non null) // null indicates that there are collisions on key values
addedEntry
IEntityStateEntry