Entity Framework "There is already an open DataReader associated with this 的解决办法

时间:2021-01-10 10:32:33

解决办法:

1,修改连接串,加上MultipleActiveResultSets=true

2, 一次性先把数据读出来

Entity Framework "There is already an open DataReader associated with this 的解决办法
var contacts = from c in db.Contact
select c; List results = contacts.ToList(); foreach (Contact c in results){
}
Entity Framework "There is already an open DataReader associated with this 的解决办法

提示:contacts.ToList() 的作用是强制加载contact列表,也就是先强制执行查询,再做后续处理。

一定要全部tolist不然上面查询结果不tolist循环里面tolist一样会出错,MS搞个鸡巴,弄个玩意这么难用