Reputation: 83356
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
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