[英]There is already an open DataReader associated with this Command
I am fairly new to link and entity framework. 我是链接和实体框架的新手。 Apparently others have had the same issue I am having and there are excellent explanations of the issue here which all make sense in theory.
显然,其他人有我有同样的问题,有问题的优秀解释在这里 ,所有有意义的理论。 However I cant seem to get the correct syntax to fix MY issue, nor do I understand it enough to decide what would be the BEST choice.
但是,我似乎无法获得正确的语法来解决我的问题,也无法充分理解它来决定什么是最佳选择。 The offending code is:
令人反感的代码是:
public void ClearAllFilesFromUser(Guid userID)
{
using (DBEntities db= new DBEntities())
{
var filez = (from p in db.Files select p);
aspnet_Users user = (from p in db.aspnet_Users
where p.UserId == userID
select p).First();
foreach (var file in filez)
{
if (file.aspnet_Users.Contains(user))
{
file.aspnet_Users.Remove(user);
}
}
db.SaveChanges();
}
}
The code produces the error at if (file.aspnet_Users.Contains(user))
. 该代码在
if (file.aspnet_Users.Contains(user))
处产生错误。 I am trying to delete all entries from the junction table in the database for a particular user. 我正在尝试从特定用户的数据库联结表中删除所有条目。 Not sure what other information is needed.
不知道需要什么其他信息。 Please advise and thanks in advance.
请提前告知并感谢。
You need to add MultipleActiveResultSets=true;
您需要添加
MultipleActiveResultSets=true;
to your connection string. 到您的连接字符串。
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Elazig-20140311114227;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Elazig-20140311114227.mdf;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
Something like this. 这样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.