We use the Linq-to-SQL DataContext in a web application that provides read-only data and never updates (to do this, force ObjectTrackingEnabled = false.
Since the data never changes (except for periodic configuration updates), it seems futile to overload it from SQL Server with a new DataContext for each web request.
We tried to cache the DataContext in the Application object for all requests, but this generated a lot of errors, and our research shows that it was a bad idea, DataContext should be disposed of within the same unit of work, an insecure stream, etc. etc.
So, since the DataContext is intended for the data access mechanism, and not for the data warehouse, we need to look for the caching of the data that we receive from it, and not the context itself.
Prefers to do this with the entities and collections themselves, so the code can be agnostic as to whether it deals with cached or "fresh" data.
How can this be done safely?
First, I need to make sure the objects and collections are fully loaded before I delete the DataContext. Is there a way to load the entire database from the database as efficiently as possible?
Secondly, I’m sure that maintaining references to entities and collections is a bad idea, because it will be
(a) cause damage to objects when the DataContext goes out of scope or
(b) preventing the DataContext from leaving the scope
So should I clone EntitySets and store them? If so, how? Or what's here?