【发布时间】:2020-10-07 22:48:47
【问题描述】:
public class Person
{
[Key]
public Guid Id { get; set; }
public string FullName { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Person> People { get; set; }
}
public class Program
{
public void Main()
{
using var context = new ApplicationDbContext(*configuration here*);
var doWorks = Enumerable.Range(0, 1000).Select(x => context.People.AsNoTracking().ToArrayAsync());
await Task.WhenAll(doWorks);
}
}
我收到此错误:
System.InvalidOperationException: '在前一个操作完成之前,在此上下文上启动了第二个操作。这通常是由使用相同 DbContext 实例的不同线程引起的。有关如何避免 DbContext 线程问题的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=2097913。'
我怎样才能运行这样的多任务并摆脱该错误?
【问题讨论】:
标签: c# asynchronous dbcontext ef-core-3.1