I’m using a Linq to Sql connection to collaborate with a database and my windows form application works with multiple threads and this dosen’t suite with the DataClass.
I modify the connection to support MultipleActiveResultSets and I get this error "An item with the same key has already been added." I have searched for this problem and got only 2 answers:
- Use your DataClass only in your main thread;
- Lock the DataClass when you retrieve a table;
I want to lock the DataClass and don’t know where to lock it, I’m sure that some one used Linq to Sql in multithreading successfully.
To expand on what I was saying in comments, and to answer your question – I would treat all access as a unit-of-work, for example (in some data-access class):
etc. Key point: the data-context is not left open indefinitely and re-used by competing threads.