2010-05-04 75 views
1

實體框架不顯示數據庫的最新更改,但在某段時間後顯示更新的內容。我沒有任何特殊的緩存在服務器或頁面上。 提前致謝。 傑克 這是我用來獲取列表中的代碼,有問題:實體框架不顯示數據庫的最新更改

var m = 
    from relation in ett.Article_Relations 
    from article in ett.Article_Articles 
    from content in ett.Article_Contents 
    where relation.MenuItemID == id 
    where relation.Article_Articles.ArticleID == article.ArticleID 
    where content.LanguageID == LanguageID 
    where article.ArticleID == content.Article_Articles.ArticleID 
    select new ArticleViewModel 
    { 
     ArticleID = article.ArticleID, 
     IsActive = article.IsActive, 
     Author = article.ArticleAuthor, 
     Content = content, 
     DateCreated = article.DateCreated 
    }; 

回答

1

有什麼不對您顯示查詢的,所以我希望你正在使用的ObjectContext比預期更長的時間。您是否在ASP.NET緩存或會話中緩存對象上下文?如果是這樣,您應該爲每個請求至少創建一次ObjectContext。永遠不要少。

事情是,實體框架ObjectContextunit of work。它在其生命週期中緩存對象。這意味着,當您查詢數據庫中已存在緩存的對象時,EF將從數據庫中檢索該值(大部分時間),但會丟棄結果並返回緩存的對象。這可以解釋爲什麼你沒有看到更新。

+0

謝謝你的回答。它給了我一些想法問題的可能性。現在我實際上通過直接在控制器中編寫代碼(我的項目在MVC中)而不是調用窗體公共類來避免(但未解決)此問題。 – Jack 2010-05-06 16:08:54