錯誤資訊:
System.Data.Entity.Core.EntityCommandExecutionException:“An error occurred while executing the command definition. See the inner exception for details.”内部異常:InvalidOperationException: 已有打開的與此 Command 相關聯的 DataReader,必須首先将它關閉。
原因分析:
dbcontext正處于一個foreach周遊過程中時,又執行其他CRUD操作。
解決辦法:
1、dbcontext在foreach周遊過程結束後,再執行其他CRUD操作。
2、使用不同的dbcontext,分别處理不同的CRUD操作。
3、将前一個DbContext的查詢結果進行緩存,如 ToList()操作。