0

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

Community
  • 1
  • 1
Darkloki
  • 676
  • 1
  • 13
  • 31

1 Answers1

0

You need to add 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.

DarthVader
  • 52,984
  • 76
  • 209
  • 300