LINQ to SQL Cache Issues

In short: I use Linq for sql, but I have problems with caching

My application is metadata driven, so I don’t want to cache (changes to db should be reflected on the website when the page is refreshed). Is there a way to disable caching? or a way to reset the cache (for example, currently, when I change the data in the database, I have to physically change the code and recompile before I see the results).

And finally, the C # question (I hope the main mistake is on my part). In the code below, if I run method1 , then method2 , then doc2 == doc1 (and I want it to get the original value from db)

It seems to me that this is very strange, since the RecordDictionary class is rotary data (therefore, it is not directly related to the model), and in my code the assignments are in different controllers; but somehow linq to sql is the caching changes applied to doc1 and applying them to doc2 (if I doc2 my application and recompile, then doc2 will be the same as what I expect from it (until I I will change doc1 ))

Given example

 public RecordDictionary method1() { RecordDictionary doc1 = genericRepository.GetRecordById( action.AppliesToModelEntityId, 27); //do some stuff to doc1 here return doc1; } public RecordDictionary method2() { RecordDictionary doc2 = genericRepository.GetRecordById( action.AppliesToModelEntityId, 27); return doc2; } public RecordDictionary GetRecordById(int ContainerModelId, int id) { var query = (from dv in _db.DataValues where dv.DataInstance.IsCurrent == true && dv.DataInstance.DataContainer.DataContainerId == id select new { dv.DataInstance.DataContainer.ParentDataContainerId, dv }); RecordDictionary result = CreateRecordColumns( ContainerModelId, query.FirstOrDefault().ParentDataContainerId); result.Id = id; foreach (var item in query) { if (result.ContainsKey(item.dv.ModelEntity.ModelEntityId)) result[item.dv.ModelEntity.ModelEntityId] = item.dv; } return result; } 
+4
source share
2 answers

Create a new DataContext for the "unit of work": i.e. An HTTP request or even a method [1] is that SubmitChanges is called once at the end of the method (or as part of an implementation).

 // Where this is all one (financial) transaction. void Transfer(int fromAccountId, int toAccountId, decimal amount) { var db = new Customers(); var fromAccount = db.Accounts.Single(row => row.Id == fromAccountId); var toAccount = db.Accounts.Single(row => row.Id == toAccountId); fromAccount.Balance -= amount; toAccount.Balance += amount; db.SubmitChanges(); } 

You can also call DataContext.Refresh () on the lines that you expected to change for DataContexts, but are difficult to use efficiently. It is intended for things like stored procedures:

 var client = db.Clients.Single(row => row.Id == clientId); if (!client.CheckingAccount) { db.CreateCheckingAccount(client.Id); // Stored procedure. db.Refresh(RefreshMode.OverwriteCurrentValues, client); } 
+6
source

Is ObjectTrackingEnabled set to true in your data data class? If so, you can try setting it to false. You can also find this blog entry .

+1
source

All Articles