Home » Ado.Net » Entity Framework » Linq to Sql and Entity Framework cache

Linq to Sql and Entity Framework cache

Linq to Sql has a caching system that rely on the primary key of each object. Inside the data context, an identity map keep the value of the retrieved data. Every thing is handled by the data context and you have to worry about nothing. Keep in mind that if someone else change the value that the cache won’t be refreshed.

If you change the data on your side, the cache won’t also be updated. This can be problematic in some situation. If this behavior is not what you want, you can disable the caching feature of Linq to Sql with a property of the data context.

myDataContext.ObjectTrackingEnabled = false;

Or, you can refresh specific object.

myDataContext.Refresh(RefreshMode.OverwriteCurrentValues, myObjectToRefresh);

The reason behind this technical choice is that initially it has been developed for performance on consecutive call of the same object. The goal is not the have a real caching system.

To conclude, the title mentions Entity Framework and I haven’t say anything about it yet. The reason is that Entity Framework react the same way as Linq to Sql for the cache.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.