Reputation: 1673
The problem I am having is as follows, due to caching issues we are putting the following before the start of every ET query:
DataBase.Refresh(System.Data.Objects.RefreshMode.ClientWins, DataBase.PublicUsers);
However, this is causing pages to take ages to load as the above command makes two calls to the database.
Does anybody know away to stop EF from caching without having to put that command before every query?
Upvotes: 1
Views: 1218
Reputation: 16800
You can set the MergeOption to all EntitieSet after create the context. Some like this:
var objSetProps = ctx.GetType().GetProperties().Where(prop => prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(ObjectSet<>));
foreach (PropertyInfo objSetProp in objSetProps)
{
ObjectQuery objSet = (ObjectQuery)objSetProp.GetValue(ctx, BindingFlags.GetProperty, null, null, null);
objSet.MergeOption = MergeOption.PreserveChanges;
}
Read about the MergeOption here: http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx Your will use NoTracking, I think.
But, iy you whant CLEAR the "cached" entities, detaching it.
var entidades = Ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);
foreach (var objectStateEntry in entidades)
Ctx.Detach(objectStateEntry.Entity);
Where Ctx are my Context.
Upvotes: 0
Reputation: 364409
To answer your initial question. If you don't want context to cache data you must execute query without change tracking.
Database.Hubs.MergeOption = MergeOption.NoTracking;
return DataBase.Hubs
.Where(h =>
h.BusinessId == null
&& h.TypeId != (int)HubType.BusinessPlace
&& h.ParentHubId != null
);
But this will not solve your architecture issue related to static / shared context in a web app. You must change your architecture if you really want to create working application.
Upvotes: 2