Entity Framework POCO Long-Term Change Tracking

I am using the .NET entity framework 4.1 with a code approach to effectively solve the following problem, it is simplified here.

  • There is a database table with tens of thousands of records.
  • Several users of my program should be able to
    • View the table (total) in GridRow, which implies that you must load the entire table.
    • Change the values โ€‹โ€‹of any random string, the changes are frequent, but not necessarily saved immediately. He expected different users to change different lines, but this is not always the case. Some loss of changes is allowed, as users are more likely to update the same rows to the same values.
    • Add new lines if necessary.

Sounds simple enough. My initial approach was to use a long instance of DbContext . This DbContext was supposed to track changes for objects, so when SaveChanges() is called, most of the work is done automatically. However, many noted that this is not an optimal solution in the long run , especially here . I'm still not sure if I understand the reasons, and I don't see that there is a unit of work in my script. The user chooses himself when to save changes, and let them say that the client always wins for simplicity. It is also important to note that objects that were not affected do not overwrite any data in the database.

Another approach would be to track changes manually or use change tracking objects for me, however, I am not very good at such methods, and I would welcome a push in the right direction.

What is the right way to solve this problem?

I understand that this question is a little desirable, but think of it as more fundamental. I lack a fundamental understanding of how to solve this class of problems. It seems to me that the long life of DbContext is the right way, but knowledgeable people tell me differently, which leads me to confusion and inaccurate questions.

EDIT1 Another confusion is the existence of the Local property of the DbSet<> object. He invites me to use a long context, as another user posted here .

+4
source share
2 answers

The problem with the long-term context is that it does not update the data - I discussed the problems more here . Therefore, if your user opens the list and changes the data in half an hour, she does not know about the changes. But in case of WPF, if your business action:

  • Open the list
  • Do as many actions as you want.
  • Saving trigger changes

Then all this is a unit of work, and for this you can use a single instance of context for this. If you have a scenario where the latest changes in the board are winning, you should not have a problem with this until someone else deletes the post that the current user is editing. In addition, after saving or reverting changes, you must re-arrange the current context and load the data - this ensures that you really have fresh data for the next unit of work.

The context offers some functions for updating data, but only updates previously downloaded data (without relationships), so, for example, new unsaved records will still be included.

Perhaps you can also read about the MS Sync structure and the local data cache.

+3
source

It sounds like your users can copy (cache) data for an indefinite period of time. The longer users use cached data, the higher the likelihood that they can be disconnected from connecting to the database in DbContext. I think EF is not up to the challenge, and you probably want to deal with this. (e.g., occlusively related architecture). I expect that implementation can solve many of your problems.

+1
source

All Articles