The context cannot be used while the model is being created

前端 未结 14 1385
渐次进展
渐次进展 2020-12-03 13:27

In my application I receive the following error:

The context cannot be used while the model is being created.

I\'m not sure what

14条回答
  •  情话喂你
    2020-12-03 14:15

    I had this error as well but was able to solve it by declaring a local variable context inside of each thread rather than using the one I had declared globally.

        Parallel.ForEach(myList, l=>
        {
            MyContext _db = new MyContext();
            // do some stuff....
            _db.Model.Add(....);
            _db.SaveChanges();
    
        });
    

    Make sure you also set MultipleActiveResultSets=true in your connection string as described above.

提交回复
热议问题