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.
- Disable Tracking using
AsNoTracking()
- Throw away the
DbContext
and create a new one - Use an
ObjectQuery
instead of aDBQuery
and setMergeOptions
- Refresh the Entities
- Detatch the Entities
- Call GetDatabaseValues to get the updated values for a single Entity
- Use the stale data
有個客戶,系統有寫帳戶管理系統給他,但因為方便,他就喜歡自己進資料庫去做新增修改
結果EF6有Cache,造成直接在資料庫新增的帳號,在前端無法登入,因為這時EF的Cache還未更新
當然我的寫法是標準寫法,並沒有考量到這種意外狀態
原來我是直接在進入Controller時就宣告一個EF Context(如果是正常流程透過程式新增帳號與設定權限,Context),而不是在每次登入檢查時宣告,造成EF Context內的Cache一直未更新
改成每次登入檢查時就Using xxx Contex=new xxx 的方式就搞定
文章標籤
全站熱搜
留言列表