Answered by:
Enterprise Entity and Caching

Question
-
I am using Enterprise Entity and I have the following statement
cudList = From p In DbContext.CleanUpDetailDatas Where p.StudyID = studyID Order By p.ConditionCriteriaID Select p
Everything works fine except there is some kind of caching going on. If I run this statement and update the data in the same record from an external source and re-run the same statement I get the previous data not the data that was just updated by an external source.
How can I tell EE to flush current contents and read the contents from the database?
Thank You
Peter
- Edited by pczurak Thursday, June 14, 2012 5:23 AM
Thursday, June 14, 2012 5:22 AM
Answers
-
Hi Peter,
The idea of Context.Refresh is to refresh the entity inside the context cache.
You can run Context.Refresh method before you do Select query then you will always get the fresh query.
e.g.
YourContext.Refresh(RefreshMode.ClientWins, yourentity);
var sel = select c from YourContext.yourentity where.... select c; // you will always get fresh entity;
Or you may consider using new instance of entity context at anywhere you deal with an entity.
Thursday, June 14, 2012 2:24 PM
All replies
-
Try this;
public static void RefreshEntity(dynamic entity) { YourContext.Refresh(RefreshMode.ClientWins, entity); }
Check this for detail reference... http://msdn.microsoft.com/en-us/library/bb348867
Cheer!
Thursday, June 14, 2012 5:53 AM -
Try this;
public static void RefreshEntity(dynamic entity) { YourContext.Refresh(RefreshMode.ClientWins, entity); }
Check this for detail reference... http://msdn.microsoft.com/en-us/library/bb348867
Cheer!
Thank you, but after I run RefreshEntity(cudList);
I get
System.InvalidOperationException: The result of a query cannot be enumerated more than once.
if I try to run foreach statement on cudList after RefreshEntity(cudList);
Plus it seems silly to run two statements to retrieve data. I am not updating data I am just reading the same record.
Peter
Thursday, June 14, 2012 2:09 PM -
Hi Peter,
The idea of Context.Refresh is to refresh the entity inside the context cache.
You can run Context.Refresh method before you do Select query then you will always get the fresh query.
e.g.
YourContext.Refresh(RefreshMode.ClientWins, yourentity);
var sel = select c from YourContext.yourentity where.... select c; // you will always get fresh entity;
Or you may consider using new instance of entity context at anywhere you deal with an entity.
Thursday, June 14, 2012 2:24 PM -
Thank you very much for your help and explanation.
That worked!!!
Peter
Thursday, June 14, 2012 3:01 PM