【问题标题】:Linq Error A second operation started on this context before a previous operation completedLinq 错误 在前一个操作完成之前在此上下文上启动了第二个操作
【发布时间】:2019-12-17 09:56:24
【问题描述】:


我想建立一个 sql 查询库。 但不幸的是,查询在子查询中不起作用。这是错误消息:
在前一个操作完成之前,第二个操作在此上下文中开始。这通常是由使用相同 DbContext 实例的不同线程引起的,但是不能保证实例成员是线程安全的。这也可能是由在客户端上评估嵌套查询引起的,如果是这种情况,请重写查询以避免嵌套调用。

这里是代码:

public class HomeController : Controller
{
    private readonly TestContext _db;

    public HomeController(TestContext db)
    {
        _db = db;
    }

    public List<Test2> GetTest2Data(int id)
    {
        return (from t2 in _db.Test2
                where t2.Id == id
                select t2).ToList();
    }

    public List<TestModel> GetTest1Data()
    {
        return (from t1 in _db.Test1
                select new TestModel
                {
                    Id = t1.Id,
                    Text = t1.Test,
                    ListTest2 = GetTest2Data(t1.Id)
                }).ToList();
    }

    public IActionResult Index()
    {
        var test = GetTest1Data();
        return View();
    }   
}

我该如何解决这个问题?
此代码有效但不可重复使用:

public class HomeController : Controller
{
    private readonly TestContext _db;

    public HomeController(TestContext db)
    {
        _db = db;
    }

    public List<TestModel> GetTest1Data()
    {
        return (from t1 in _db.Test1
                select new TestModel
                {
                    Id = t1.Id,
                    Text = t1.Test,
                    ListTest2 = (from t2 in _db.Test2
                                 where t2.Id == t1.Id
                                 select t2).ToList();
                }).ToList();
    }

    public IActionResult Index()
    {
        var test = GetTest1Data();
        return View();
    }   
}

型号:

public class Test1
{
    public int Id { get; set; }
    [StringLength(50)]
    public string Test { get; set; }
    public int? First { get; set; }
    public int? Second { get; set; }
}
public class Test2
{
    public int Id { get; set; }        
    [StringLength(50)]
    public string Test2 { get; set; }
    public int? First { get; set; }
    public int? Second { get; set; }
}
public class TestModel
{
    public int Id { get; set; }
    public string Text { get; set; }
    public List<Test2> ListTest2 { get; set; }
}

#Update 1

public List<TestModel> GetTest1Data()
{
    return (from t1 in _db.Test1
        select new TestModel
        {
            Id = t1.Id,
            Text = t1.Test
        }).ToList()
        .Select(t1 => {
            t1.ListTest2 = GetTest2Data(t1.Id);
            return t1;
        }).ToList();
}

【问题讨论】:

  • 您能否详细介绍一下 Test1 和 Test2 在底层数据模型中的关系?
  • 我已将模型添加到问题中...它们是简单的虚拟模型。

标签: linq asp.net-core-mvc entity-framework-core


【解决方案1】:

要提出的建议不止一个,但为了让事情变得简单,以便快速解决您的问题,您只需根据上下文使用 ToList() 更改检索数据的位置。

public List<TestModel> GetTest1Data()
{
    return (from t1 in _db.Test1)
           .ToList()
           .Select(t => new TestModel
            {
                Id = t1.Id,
                Text = t1.Test,
                ListTest2 = GetTest2Data(t1.Id)
            }).ToList();
}

这基本上将检索数据,完成第一个操作,然后分别调用 Test2Data,这样您就不会在上下文中执行并发操作。也就是说,请记住,它将在每个 Test1 记录上调用 Test2Data。毕竟这只是一个测试:)

HTH

【讨论】:

  • 这是正确的方法。我已将上面的解决方案代码作为 Update1 添加到问题中。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 2022-01-01
  • 2020-01-22
  • 1970-01-01
  • 2014-12-08
  • 2021-05-04
  • 2018-11-07
相关资源
最近更新 更多