Invalidoperationexception:已经有一个与此命令关联的打开的datareader,必须先关闭它。使用ASP.NET身份 [英] Invalidoperationexception: there is already an open datareader associated with this command which must be closed first. With ASP.NET identity

查看:95
本文介绍了Invalidoperationexception:已经有一个与此命令关联的打开的datareader,必须先关闭它。使用ASP.NET身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库返回数据:





I'm trying to return data from database:


public List<DataModel> GET()
    {
        foreach (var users in db.Users) {
            var user = db.Users.Select(x => new DataModel()
            {
                Email = users.Email,
                UserName = users.UserName,
                FirstName = users.FirstName,
                LastName = users.LastName,
                Age = users.Age,
                Phone = users.Phone,
                Department = users.Department
            });
            return user.ToList();

        }





这是db class:





Here is db class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}









这是错误:







Here is the error:

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





你能告诉我这段代码有什么问题吗?



我尝试了什么:



我在数据库中有四条记录,以下代码返回四条记录但是有第一条记录数据!!





Could you please tell what's wrong with this code?

What I have tried:

I have four record in database and the following code return four record but with first record data !!

public List<DataModel> GET()
           {


                   var user = db.Users.Select(x => new DataModel()
                   {
                       Email = db.Users.FirstOrDefault().Email,
                       UserName = db.Users.FirstOrDefault().UserName,
                       FirstName = db.Users.FirstOrDefault().FirstName,
                       LastName = db.Users.FirstOrDefault().LastName,
                       Age = db.Users.FirstOrDefault().Age,
                       Phone = db.Users.FirstOrDefault().Phone,
                       Department = db.Users.FirstOrDefault().Department
                   });
                   return user.ToList();

           }

推荐答案

这可能是因为这行



It's probably because this line

foreach (var users in db.Users)





打开一个连接,这一行





opens a connection and this line

var user = db.Users.Select(





设置第二个连接,当你在下一行使用.ToList时激活它。



你实际上不需要这两个循环,这是未经测试但尝试类似





sets up a second connection which is activated when you use .ToList on the following line.

You don't actually need the two loops, this is untested but try something like

public List<DataModel> GET()
    {
            var user = db.Users.Select(x => new DataModel()
            {
                Email = x.Email,
                UserName = x.UserName,
                FirstName = x.FirstName,
                LastName = x.LastName,
                Age = x.Age,
                Phone = x.Phone,
                Department = x.Department
            });

            return user.ToList();

        }


这篇关于Invalidoperationexception:已经有一个与此命令关联的打开的datareader,必须先关闭它。使用ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆