I think that perhaps the original question was too long-winded with too many unnecessary details, so this is my attempt to simplify.
I am looking for a means to perform any of the actions below. I only need to do one, not all. If anyone knows the answer to even one of these, please respond. So, is it possible to do any of the following in Linq to SQL:
Pull entities out of a
DataContext
viaExecuteQuery
orExecuteMethodCall
without having those entities tracked?Invoke
ExecuteQuery
orExecuteMethodCall
and guarantee that I always receive fresh copies of the results retrieved from the database, even if those entities had already been retrieved and are already in the identity cache?Instruct Linq to SQL not to perform any change tracking whatsoever on specific entity types - but still allow change tracking for other types?
Restrictions:
The
Refresh
method is out of the question; the number of entities is quite large and this would become a performance disaster.I cannot simply set
ObjectTrackingEnabled
tofalse
, because theDataContext
does not allow setting it back totrue
after a query has been executed, and I do need some of the entities to be tracked.I also cannot throw away the original
DataContext
and use a new one; I need to be able to do this in the middle of a transaction.
This is starting to become a serious problem, and I really think that the default behaviour is ill-conceived. If I execute an ad-hoc query or stored procedure, I expect the results I receive to be the exact results that were returned by said query. It only makes sense; if I wanted the old, stale entities, why would I go back to the database to get them?
At the moment, my workaround is to either (a) create a new DataContext
specially for the query and override the transaction isolation level, or (b) make the return type a "DTO" that is identical to the entity in every way but without the [Table]
attribute, and map it to the original entity using AutoMapper. Both of these seem like horrible hacks.
Would really appreciate any suggestions anyone has on this conundrum.