【发布时间】:2019-01-02 20:21:38
【问题描述】:
我的数据库中有一个名为 devices 的表,EF 在我的项目中创建了该表的模型。我有一个称为元素的字符串列表,我需要通过这个元素循环异步并进行检查,然后根据结果更新设备对象的属性,在每次迭代开始时我创建设备模型的新实例,在准备好我需要的设备对象之后在我的数据库中查询我的设备表以获取相同元素的所有记录,然后根据新对象更新所有记录。当我尝试在我的数据库中查询我的设备表时,我收到了这个异常
System.InvalidOperationException: '在创建模型时不能使用上下文。如果在 OnModelCreating 方法中使用上下文,或者多个线程同时访问同一个上下文实例,则可能会引发此异常。请注意,不保证 DbContext 和相关类的实例成员是线程安全的。'
这是我的代码流程
static async void Begin(List<string> elements)
{
ParalelForEach(elements, element => {
devices devObj = new devices();
//here i do my operation on element and update devObj object
// now in below i should query my device table in my database to collect all records of same element so I can update them based on new devObj i created but above Exception thrown
var dbElementsList = dbContext.devices.Where(e =>e.name == element).Tolist();//Exception is thrown here
foreach(var dbe in dbElementsList)
{
//update process happens here on dbe
}
dbContext.devices.Add(devObj);
dbContext.SaveChanges();
}):
}
【问题讨论】:
-
这个问题的答案可能对你有用:stackoverflow.com/questions/12827599/…
标签: c# asp.net-mvc asynchronous