Is there a way to include locally cached items in a DBSet query?

I just pee my feet using the Entity Framework, but I'm a bit puzzled by some DBSet object behavior. When I call the Find () method, it seems to know my collection of recently added (but not yet saved) elements, but when I try to query the DBSet, it seems that they include only those elements that were there all the time. Is there a simple way to get around this? I include some code that I did to try to get around this, but when it starts iterating elements from my "Added" change set, I get this error:

"It is not possible to create a constant value of type EntityType. Only primitive types (such as Int32, String, and Guid) are supported in this context."

internal DbContext Context  { get; set; }
protected DbSet<T> DBSet    { get; set; }

public virtual IEnumerable<T> Get(
    Expression<Func<T, bool>> filter = null,
    Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
    bool ignoreCachedChanges = false)
{
    IQueryable<T> oReturn = DBSet;

    // This block is intended to adjust the queryable source to account for changes
    if (!ignoreCachedChanges)
    {
        oReturn = oReturn.Except(Context.ChangeTracker.Entries<T>().Where(x => x.State == System.Data.EntityState.Deleted).Select(x => x.Entity));
        oReturn = oReturn.Union(Context.ChangeTracker.Entries<T>().Where(x => x.State == System.Data.EntityState.Added).Select(x => x.Entity));
    }
    if (filter != null)
        oReturn = oReturn.Where(filter);

    if (orderBy != null)
        return orderBy(oReturn).ToList();
    else
        return oReturn.ToList();
}

// NOTE: Get By ID uses Find which considers the queued-but-not-yet-applied changes
public virtual T GetByID(object id)
{
    return DBSet.Find(id);
}

( , , , unit test, () , ... , , , ... , , - FindByID, , , , ... ?:))

+5
1

: ( T) Except Union SQL "" . Except Union LINQ to Objects, : , (, - .ToList()). , Added.

+2

All Articles