Continuing the extension methods series, here are two more methods that make it easier to work with the Entity Framework cache.
C#
public static IEnumerable<T> GetEntitiesInCache<T>(this ObjectContext context)
{
return GetEntitiesInCache<T>(context, EntityState.Added | EntityState.Modified | EntityState.Unchanged);
}
public static IEnumerable<T> GetEntitiesInCache<T>(this ObjectContext context, EntityState state)
{
Contract.Requires(context != null);
return context.ObjectStateManager.GetObjectStateEntries(state).Select(objectStateEntry => objectStateEntry.Entity).OfType<T>();
}
Usage examples:
C#
context.GetEntitiesInCache<Person>();
context.GetEntitiesInCache<Person>(EntityState.Added);
context.GetEntitiesInCache<Person>(EntityState.Added | EntityState.Modified);
Do you have a question or a suggestion about this post? Contact me!