Dot-Net

Entity Framework 4.1 Code First 中的 Multipleactiveresultsets

  • February 8, 2016

這是我的第一個 EF 項目,請多多包涵。

在更新諸如 Department 之類的實體時,您將其從上下文中拉出,更新其值並呼叫 context.SaveChanges。但是,如果您更新 Department.Employees,EF 不會覺得這很有趣。

我搜尋並想出了在連接字元串中設置 Multipleactiveresultsets=true 的選項,但想知道是否:

  • 這是推薦的方式嗎?
  • 這是否會對性能產生不利影響/我應該注意什麼?

僅當您想在同一連接上並行執行多個查詢時,才需要啟用 MARS。如果您執行以下操作,則會發生這種情況:

/* Foreach uses an iterator over the resultset of your query, but the query is not fetched
  immediately, instead the iterator internally triggers fetching for single
  processed record from opened data reader. Because of that the query and the reader
  are active until the iteration is over. */
foreach (var department in context.Departments.Where(...))
{
   /* The first query is still active on the connection but now you are executing
      lazy loading of all related employees =>. You are executing a second query and,
      without MARS, you will get an exception. */
   var employee = department.Employees.FirstOrDefault(...);
}

如何避免這種情況?

  • 使用急切載入而不是延遲載入:context.Departments.Include(d => d.Employees)
  • 在使用延遲載入之前實現整個部門的結果集。這意味著不訪問循環內的員工。
  • 啟用 MARS,上面提到的範例將簡單地工作

這是推薦的方式嗎?這是否會對性能產生不利影響/我應該注意什麼?

這取決於您要解決的問題。如果您有多個部門要處理,訪問他們的員工集合將觸發每個部門的單獨查詢。這被稱為 N+1 問題 - 您有 N 個部門和一個查詢來獲取它們,並且對於每個部門,您將執行一個額外的查詢 => N+1 個查詢。對於大量部門來說,這將是一個性能殺手。

急切載入也不是一個防彈解決方案。它也會影響性能。有時您只需要執行單獨的查詢來獲取所有必要的部門,並執行單獨的查詢來獲取所有必要的員工。如果您關閉了延遲載入,它應該會修復您的關係並為您正確填寫員工屬性。順便說一句,我對 Data UserVoice 提出了一個建議,即開箱即用地支持此功能。

引用自:https://stackoverflow.com/questions/10478693