[英]LINQ to SQL cache issues
長話短說:我正在使用Linq來進行sql,但是我的緩存存在問題
我的應用程序是元數據驅動的,所以我不希望緩存(數據庫中的更改應該反映在頁面刷新的網站上)。 有沒有辦法關閉緩存? 或者一種重置緩存的方法(例如,當我在數據庫中更改數據時,我必須在物理上更改代碼並在看到結果之前重新編譯)。
最后是ac#question(希望是我的一個基本錯誤)。 在下面的代碼中,如果我運行method1
然后是method2
那么doc2 == doc1
(我希望它從db獲取原始值)
這對我來說似乎很奇怪,因為RecordDictionary
類是數據RecordDictionary
(因此不直接與模型相關),在我的代碼中,賦值在不同的控制器中; 但不知何故,LINQ to SQL的是應用到緩存的變化doc1
並將它們應用到doc2
(如果我退出了我的應用程序,並重新編譯然后doc2
等於什么,我希望它是(直到我改變doc1
)
舉例說明
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;
}
每個“工作單元”創建一個新的DataContext:即HTTP請求,甚至是方法[1] - 意圖是在方法結束時(或作為實現的一部分)調用SubmitChanges一次。
// 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();
}
您還可以在DataContexts后面的行上調用DataContext.Refresh(),但很難高效使用。 它適用於存儲過程之類的東西:
var client = db.Clients.Single(row => row.Id == clientId);
if (!client.CheckingAccount) {
db.CreateCheckingAccount(client.Id); // Stored procedure.
db.Refresh(RefreshMode.OverwriteCurrentValues, client);
}
ObjectTrackingEnabled是否在您的數據上下文類中設置為true? 如果是這樣,您可能想嘗試將其設置為false。 此外,您可能會發現閱讀此博客條目很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.