How do you update the ObjectContext cache from the database?

.net c# entity-framework objectcontext

Question

DB data is being loaded:

var somethings = Context.SomethingSet.ToList();

Then someone adds or deletes rows that are not relevant. Because it doesn't know they were destroyed, Out context nevertheless stores deleted objects. even if Context is called. Our context still includes removed items, and the navigation attributes are incorrect. SomethingSet.ToList().

What is the best way to update the whole collection from the database?

1
15
2/25/2010 3:06:18 AM

Accepted Answer

What you need is the Refresh technique.

Context.Refresh(RefreshMode.StoreWins, somethings);
19
2/25/2010 2:44:52 AM

Popular Answer

The Unit of Work pattern is implemented in the EF data context. As a result, it is NOT intended to be used after the unit of work has been completed. The idea is that your data context will be removed after your job is finished.

This is a basic architectural choice that applies to both EF v1 and v4 as well as LINQ to SQL. You should refrain from keeping your data contexts around longer than strictly necessary to accomplish your unit of work, unless you have really particular data consumption patterns and large amounts of memory.

http://sdesmedt.wordpress.com/2009/02/18/unit-of-work-pattern/

http://takacsot.freeblog.hu/Files/martinfowler/unitOfWork.html



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow