Get newly added entities in an Entity Framework query

A few days ago, a colleague of mine ran into a little problem using Entity Framework. He was adding new entities to the context and then wanted to display these entities. Unfortunately, since the new entities are only in memory, they are not fetched by the query.
In fact, you have to use the ObjectStateManager accessible from the context to query in-memory entities. This object manages the entity cache in the context.

You can use the following code to obtain every entity already in the cache. Assuming the type of the objects you want to query is Client :
var listInMemory =
context.ObjectStateManager
.GetObjectStateEntries(EntityState.Added)
.Select(e => e.Entity).OfType<Client>().ToList();


listInMemory will contain only the new added entities from the current context. You can combine EntityState values to retrieve entities in different states.

You can also use an Union construct to add those entities to the list of entities returned by a standard Entity query. For example :
var completeList =
context.Clients.AsEnumerable()
.Union(context.ObjectStateManager.GetObjectStateEntries
(EntityState.Added)
.Select(e => e.Entity)
.OfType<client>()).ToList();

You may also want to filter out the deleted records from the context.Clients by using a Where clause just before the AsEnumerable(). I leave that as an exercice to the reader :)

Note the use of AsEnumerable(). It is related to the way LINQ to Entities handles comparisons (for the Union method in this case). You can find more information about comparisons on the MSDN.

Comments

  1. Thanks for posting this, I ran into the same problem and this saved me some time!

    ReplyDelete
  2. great example... saves a lot of time :) thanx

    ReplyDelete
  3. you are a genious!! thanks this helped me a lot!

    ReplyDelete
  4. Is below query equivalent to Union Query used above?

    var completeList =
    (context.ObjectStateManager.GetObjectStateEntries
    (EntityState.Added | EntityState.Unchanged)
    .Select(e => e.Entity)
    .OfType()).ToList();

    ReplyDelete
    Replies
    1. I think so but I am not sure this code is still relevant for the latest EF versions. There may or may not be a better way to do this.

      Delete
    2. Thanks for your quick reply. I tried it with 4.0. During load it returns null, in all other cases it has similar behavior with union statement.

      Delete

Post a Comment

Popular posts from this blog

Create a draft release and start it using the TFS REST API

Adding a delay before processing Textbox events

Change the deployment URL of a ClickOnce application