简体   繁体   English

已经有与此命令关联的打开的DataReader

[英]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.

相关问题 C#:已经有与此命令关联的打开的DataReader - C# : there is already an open DataReader associated with this command 已经有一个打开的 DataReader 与此命令关联,没有 ToList() - There is already an open DataReader associated with this Command without ToList() 已经与该命令关联的打开的DataReader-当我不使用datareader时? - already an open DataReader associated with this Command - when I'm not using datareader? 已经有一个打开的 DataReader 关联 - There is already an open DataReader associated 已经有与此命令关联的打开的DataReader,没有嵌套的datareader - There is already an open DataReader associated with this Command, without nested datareaders Json序列化错误“此命令已存在一个打开的DataReader” - Error in Json Serialization “There is already an open DataReader associated with this Command” 实体框架错误:已经有一个打开的 DataReader 与此命令关联 - Entity Framework error: There is already an open DataReader associated with this Command 已与此连接关联的打开的DataReader - Already an open DataReader associated with this Connection 另一个“已经有一个开放的DataReader关联......” - Another “There is already an open DataReader associated…” C#中已经有一个关联的打开的Datareader - There is already an open Datareader associated in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM