views:

141

answers:

2

Could I store DataContext for a long time? What would be with Connection?

+1  A: 

Some links that I found useful:

alexandrul
The fourth link is probably the quintessence of it. The short answer: "you can, but it's not a good idea".
Pavel Minaev
+1  A: 

A data-context should generally be a short-lived unit of work; otherwise you have a number of data-growth issues with the identity manager and change tracking (cache). Additionally, you will start getting stale objects, especially if you have multiple machines looking at the database (a web farm for example).

Personally, I limit it to inside the repository layer, so the data-context only exists for the duration of the actual data operation.

The connection/transaction is another consideration re scalability, but that will be less immediate than seeing old data and getting vast numbers of concurrency errors.

Regarding the connection; SqlConnection uses connection pooling anyway; so there is very little difference in having a single connection vs several sequential connections (with the same connection string). There is a good chance that they'll use the same database connection (SPID).

Marc Gravell
Connection could disappear, and DataContext wont reopen it and will not get new
dynback.com