close

底下是處理EF Cache的幾個方法

There are several ways to manage this, depending on which version of Entity Framework you are using and what type of application you are writing.

  1. Disable Tracking using AsNoTracking()
  2. Throw away the DbContext and create a new one
  3. Use an ObjectQuery instead of a DBQuery and set MergeOptions
  4. Refresh the Entities
  5. Detatch the Entities
  6. Call GetDatabaseValues to get the updated values for a single Entity
  7. Use the stale data

有個客戶,系統有寫帳戶管理系統給他,但因為方便,他就喜歡自己進資料庫去做新增修改

結果EF6有Cache,造成直接在資料庫新增的帳號,在前端無法登入,因為這時EF的Cache還未更新

當然我的寫法是標準寫法,並沒有考量到這種意外狀態

原來我是直接在進入Controller時就宣告一個EF Context(如果是正常流程透過程式新增帳號與設定權限,Context),而不是在每次登入檢查時宣告,造成EF Context內的Cache一直未更新

 

改成每次登入檢查時就Using xxx Contex=new xxx 的方式就搞定

 

arrow
arrow
    文章標籤
    Entity Framework Cache
    全站熱搜

    carlpan 發表在 痞客邦 留言(0) 人氣()