The operation cannot be completed because the DbContext has been disposed error

后端 未结 8 2061
情歌与酒
情歌与酒 2020-11-29 09:22

I\'m new to EF and I\'m trying to use an extension method which converts from my Database type User to my info class UserInfo.
I\'m using data

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 09:37

    This question & answer lead me to believe that IQueryable require an active context for its operation. That means you should try this instead:

    try
    {
        IQueryable users;
    
        using (var dataContext = new dataContext())
        {
            users = dataContext.Users.Where(x => x.AccountID == accountId && x.IsAdmin == false);
    
            if(users.Any() == false)
            {
                return null;
            }
            else
            {
                return users.Select(x => x.ToInfo()).ToList(); // this line is the problem
            }
        }
    
    
    }
    catch (Exception ex)
    {
        ...
    }
    

提交回复
热议问题