错误信息:
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()操作。