There is already an open DataReader associated with this Command which must be closed first

前端 未结 18 2320
孤街浪徒
孤街浪徒 2020-11-22 01:40

I have this query and I get the error in this function:

var accounts = from account in context.Accounts
               from guranteer in account.Gurantors
           


        
18条回答
  •  -上瘾入骨i
    2020-11-22 02:16

    In my case, using Include() solved this error and depending on the situation can be a lot more efficient then issuing multiple queries when it can all be queried at once with a join.

    IEnumerable users = db.Users.Include("Projects.Tasks.Messages");
    
    foreach (User user in users)
    {
        Console.WriteLine(user.Name);
        foreach (Project project in user.Projects)
        {
            Console.WriteLine("\t"+project.Name);
            foreach (Task task in project.Tasks)
            {
                Console.WriteLine("\t\t" + task.Subject);
                foreach (Message message in task.Messages)
                {
                    Console.WriteLine("\t\t\t" + message.Text);
                }
            }
        }
    }
    

提交回复
热议问题