Fixing the "There is already an open DataReader associated with this Command which must be closed first." exception in Entity Framework 下面代码可能会报如标题所示错误 var contacts = from c in db.Contact select c; foreach (Contact c in contacts) { if (c.Phones.…
在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”. public class User { public long UserId { get; set; } public string UserName { get; set; } public string UserPwd { get; set; } public D…
执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,会得到一个错误提示:There is already an open DataReader associated with this Command which must be closed first.,然后一般就会产生数据保存失败的异常. 解决方法是在ConnectionString中加上一个参数“MultipleActiveResultSets”, 将其值设置为tru…
原文:https://www.cnblogs.com/sdusrz/p/4433108.html 执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,会得到一个错误提示:There is already an open DataReader associated with this Command which must be closed first.,然后一般就会产生数据保存失败的异常. 解决方法是在ConnectionS…
This can be easily solved by allowing MARS in your connection string. Add MultipleActiveResultSets=true to the provider part of your connection string (where Data Source, Initial Catalog, etc. are specified). http://msdn.microsoft.com/en-us/library/h…
通常出现在嵌套查询数据库(比如在一个qry的遍历时,又进行了数据库查询) 通过在连接字符串中允许MARS可以轻松解决这个问题. 将MultipleActiveResultSets = true添加到连接字符串的提供程序部分 "SqlServerConnection": "Server=.\\sqlexpress;Database=test;Integrated Security=True;Connect Timeout=15;MultipleActiveResultSets=…
使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 There is already an open DataReader associated with this Connection which must be closed first 解决方法如下红色所示: @foreach (var item in Model.ToList()) {   @Html.ActionLink(@item.user.nickname + "(微信)", "…
[参考]There is already an open datareader associated with this command引发的问题 我在语句中并未使用 DataReader,未何也提示同样的错误,这个DataReader隐藏在哪里,我给大家在这里指出来,由于本人研究的还不够深入,只知道有一种方法的调用后会生成 DataReader,我想这也是绝大多数人遇到头疼的问题. 在使用数据库更新或插入语句时,大家通常使用 SqlCommand 的 ExecuteNonQuery() 方法,…
捕捉到 System.InvalidOperationException _HResult=-2146233079 _message=意外的连接状态.在使用包装提供程序时,请确保在已包装的 DbConnection 上实现 StateChange 事件. HResult=-2146233079 解决参考: https://stackoverflow.com/questions/4867602/entity-framework-there-is-already-an-open-datareader…
解决办法: 1,修改连接串,加上MultipleActiveResultSets=true 2, 一次性先把数据读出来 var contacts = from c in db.Contact select c; List results = contacts.ToList(); foreach (Contact c in results){ } 提示:contacts.ToList() 的作用是强制加载contact列表,也就是先强制执行查询,再做后续处理. 一定要全部tolist不然上面查询结…