【问题标题】:There is already an open DataReader associated with this Command已经有一个打开的 DataReader 与此命令关联
【发布时间】:2014-04-29 17:05:07
【问题描述】:

我对链接和实体框架相当陌生。显然,其他人也遇到了与我相同的问题,并且对问题 here 有很好的解释,这在理论上都是有意义的。但是我似乎无法获得正确的语法来解决我的问题,我也没有足够的理解来决定什么是最好的选择。违规代码是:

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();
    } 
}

代码在if (file.aspnet_Users.Contains(user)) 处产生错误。我正在尝试从特定用户的数据库中的联结表中删除所有条目。不确定还需要什么其他信息。请提前告知和感谢。

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4


    【解决方案1】:

    您需要将MultipleActiveResultSets=true; 添加到您的连接字符串中。

     <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>
    

    类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2011-08-29
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多