I have a project with a number of different classes querying and modifying data in a common set of tables. I’ve set up a .dbml file which provides us with a DataContext class. My question is whether a single instance of the DataContext should be used by all objects, or whether multiple instances are safe to use. I’m also wondering about thread safety in the case of a single DataContext, and whether access to it’s methods should be synchronized.
Share
Rick Strahl has a nice article about your options: http://www.west-wind.com/weblog/posts/246222.aspx.
See also: LINQ to SQL – where does your DataContext live?.
You may want a slightly different strategy for each type of deployment – web, desktop, windows service…
Summarized, your options are:
I opted for a DataContext per data object. It may not be the fanciest solution but it works in all deployment environments.