I have an Entity Framework dbContext
and method which makes some manipulations with database. How to properly call it from multiple threads to avoid deadlocks, connection errors and so on? I tried it different ways, and I had a lot of exceptions.
public void Foo(Bar bar)
{
using (var db = new ApplicationDbContext())
{
db.Documents.Add(bar);
...
db.SaveChanges();
}
}
static void Main(string[] args)
{
...
var t1 = new Thread(()=>Foo(bar1));
thread.Start();
var t2 = new Thread(()=>Foo(bar2));
thread.Start();
...
}
I never used threads before. Thanks for your help.