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.

Popular posts from this blog

Handling exceptions the right way in WCF (part 2)

Adding a delay before processing Textbox events

Change the deployment URL of a ClickOnce application