/// <summary>
        ///     Finds an entity in the state manager with the given primary key values, or returns null
        ///     if no such entity can be found.  This includes looking for Added entities with the given
        ///     key values.
        /// </summary>
        private object FindInStateManager(WrappedEntityKey key)
        {
            DebugCheck.NotNull(key);

            // If the key has null values, then it cannot be in the state manager in anything other
            // than the Added state and we cannot create an EntityKey for it, so skip the first check.
            if (!key.HasNullValues)
            {
                // First lookup non-added entries by key.  Added entries won't be found this way because they
                // have temp keys.
                ObjectStateEntry stateEntry;
                if (InternalContext.ObjectContext.ObjectStateManager.TryGetObjectStateEntry(
                        key.EntityKey, out stateEntry))
                {
                    return(stateEntry.Entity);
                }
            }

            // If we didn't find it that way, then look through all the Added entries.  In this case we
            // need to look at the key values in entity itself because temp keys don't contain any useful
            // information.
            object entity = null;

            foreach (
                var addedEntry in
                from e in InternalContext.ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
                where !e.IsRelationship &&
                e.Entity != null &&
                EntitySetBaseType.IsAssignableFrom(e.Entity.GetType())
                select e)
            {
                var match = true;
                // Note that key names from the entity set and CurrentValues are both c-space, so we don't need any mapping here.
                foreach (var keyProperty in key.KeyValuePairs)
                {
                    var ordinal = addedEntry.CurrentValues.GetOrdinal(keyProperty.Key);
                    if (!DbHelpers.KeyValuesEqual(keyProperty.Value, addedEntry.CurrentValues.GetValue(ordinal)))
                    {
                        match = false;
                        break;
                    }
                }

                if (match)
                {
                    if (entity != null)
                    {
                        throw Error.DbSet_MultipleAddedEntitiesFound();
                    }
                    entity = addedEntry.Entity;
                }
            }

            // May still be null
            return(entity);
        }
Example #2
0
        private object FindInStateManager(WrappedEntityKey key)
        {
            ObjectStateEntry entry;

            if (!key.HasNullValues && this.InternalContext.ObjectContext.ObjectStateManager.TryGetObjectStateEntry(key.EntityKey, out entry))
            {
                return(entry.Entity);
            }
            object obj = (object)null;

            foreach (ObjectStateEntry objectStateEntry in this.InternalContext.ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Where <ObjectStateEntry>((Func <ObjectStateEntry, bool>)(e =>
            {
                if (!e.IsRelationship && e.Entity != null)
                {
                    return(this.EntitySetBaseType.IsAssignableFrom(e.Entity.GetType()));
                }
                return(false);
            })))
            {
                bool flag = true;
                foreach (KeyValuePair <string, object> keyValuePair in key.KeyValuePairs)
                {
                    int ordinal = objectStateEntry.CurrentValues.GetOrdinal(keyValuePair.Key);
                    if (!DbHelpers.KeyValuesEqual(keyValuePair.Value, objectStateEntry.CurrentValues.GetValue(ordinal)))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    if (obj != null)
                    {
                        throw Error.DbSet_MultipleAddedEntitiesFound();
                    }
                    obj = objectStateEntry.Entity;
                }
            }
            return(obj);
        }