The context cannot be used while the model is being created

前端 未结 14 1340
渐次进展
渐次进展 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:13

    I had the same issue and solved it using following code:

    Database.SetInitializer<EntitiesName>(null);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题