I'm using .NET entity framework 4.1 with code-first approach to effectively solve the following problem, here simplified.
- There's a database table with tens of thousands of entries.
- Several users of my program need to be able to
- View the (entire) table in a GridRow, which implied that the entire Table has to be downloaded.
- Modify values of any random row, changes are frequent but need not be persisted immediately. It's expected that different users will modify different rows, but this is not always true. Some loss of changes is permitted, as users will most likely update same rows to same values.
- On occasion add new rows.
Sounds simple enough. My initial approach was to use a long-running DbContext
instance. This one DbContext
was supposed to track changes to the entities, so that when SaveChanges()
is called, most of the legwork is done automatically. However many have pointed out that this is not an optimal solution in the long run, notably here. I'm still not sure if I understand the reasons, and I don't see what a unit-of-work is in my scenario either. The user chooses herself when to persist changes, and let's say that client always wins for simplicity. It's also important to note that objects that have not been touched don't overwrite any data in the database.
Another approach would be to track changes manually or use objects that track changes for me, however I'm not too familiar with such techniques, and I would welcome a nudge in the right direction.
What's the correct way to solve this problem?
I understand that this question is a bit wishy-washy, but think of it as more fundamental. I lack fundamental understanding about how to solve this class of problems. It seems to me that long living DbContext
is the right way, but knowledgeable people tell me otherwise, which leads me to confusion and imprecise questions.
EDIT1
Another point of confusion is the existance of Local
property on the DbSet<>
object. It invites me to use a long running context, as another user has posted here.