Adam Rackis
Adam Rackis

Reputation: 83356

Entity Framework 4 - prevent basic caching

If I have an entity framework 4 context, normally if I have run a query like:

db.Invoices.Where(I => whatever);

and then later run something else:

db.Invoices.Where(I => something_else);

any objects returned from the second query, which were also returned from the first, will not be loaded from the database. EF will just take the object it already has for that key.

Question, what is the best way to turn this feature off? Can I tell EF to load each and every object it needs from the database without exception?

Thanks!

Upvotes: 10

Views: 5296

Answers (1)

Simon Steele
Simon Steele

Reputation: 11608

You need to change the MergeOption setting for your entity, e.g.

db.Invoices.MergeOption = MergeOption.OverwriteChanges;

OverwriteChanges means that objects are always loaded from the data source. You can also use NoTracking to disable tracking completely, which can improve performance if you don't need to make updates to the data or re-use the queries. The default value is AppendOnly which has the behaviour you've observed.

Upvotes: 9

Related Questions