“System.Data.Entity.Core.EntityCommandExecutionException”類型的異常在 EntityFramework.SqlServer.dll 中發生,但未在用戶代碼中進行處理
其他信息: 執行命令定義時出錯。有關詳細信息,請參閱內部異常。
找到詳細:
entity framework中 已有打開的與此 Command 相關聯的 DataReader,必須首先將它關閉。
EF內部是使用DataReader作資料存取,所以如果如下就會產生Error,因為DataReader用同一個Connection.
處理方式有兩種
1,設定ConnectionString加上MultipleActiveResultSets=true,但只適用於SQL 2005以後之版本
2.先讀出放置在List中
foreach(Employee emp in context.Employees.ToList())
{
emp .Customer .... //通過,因emp這時候已經不是context中的參考物件,而是獨立的instance,與context脫鉤
}