2

I'm trying to update parts of my entity framework 6 entities, specifically collections with data from the database. Reading the data from the database the first time is fast so it should be possible to read it again quickly.

Disposing the context and creating a new one is fast, but it doesn't work for me, because doing so would lead to losses of information added by the user.

So in this case, I have a class called Evaluation, Evaluation has a many-to-many relationship through the class Amount to Classification. Classification in turn has n-1 relationship to Substance, i.e. one Substance can have multiple Classifications.

Evaluations and Amounts are handled in program A, while Classifications and Substances are handled in program B. The users frequently do updates in program B, which they must be able to see in program A, while a context is already opened.

In my user interface, the user adds Classifications to Evaluations, the user first selects a Substance from a datagrid, a second datagrid then shows the Classifications related to the selected Substance.

The substances are updated as they should, because the wpf binding is to a property that will always read from the database (using a few search parameters), so if I add or remove substances, they are added or removed in the UI as well.)

I managed to refresh Classifications as well, by binding to a property that returns:

_context.Classifications.Where(c => c.SubstanceID == SelectedSubstance.SubstanceID).ToList();

instead of just binding to the navigational property of the Substance.

In this way, additions and removals of Classifications are noticed in the UI. If I change a Classification property (that is shown in the datagrid), that change is not noticed though. And it's not a matter of the UI not updating, the View Model has the old value as well.

I can update the properties by doing:

_context.Entry(Classification).Reload(); // very slow for a single entity!

A Classification also has a n-m relationship to RiskPhrase through the entity ClassificationToRiskPhrase, these riskphrases are shown in the datagrid through a converter. At first, I wasn't able to get either the added or removed ClassificationToRiskPhrases, but doing this:

_context.Entry(Classification).Collection(c => c.ClassificationToRiskPhrases).Query().ToList());

Makes the additions appear, but if I remove a ClassificationToRiskPhrase, this isn't shown.

So my first question is: How do I update an entity so that removals in one of its collection navigational properties is noticed?

My second question is more general: What's a good way to handle partial refreshes in general, does anyone know of literature in any form which covers it? I guess it's common to keep a context alive during the time a user edits a certain entity? I know short lived contexts are prefered, but to keep all changes from being automatically saved, one has to wait before doing context.SaveChanges().

Mårten
  • 231
  • 2
  • 14
  • 1
    possible duplicate http://stackoverflow.com/questions/20270599/entity-framework-refresh-context – Bhavik Patel Sep 22 '15 at 06:24
  • Did you even read my post? I have used solutions from that post, it solved some of my issues, as I mentioned, it did not solve the issues that I asked about. – Mårten Sep 22 '15 at 07:03
  • Generally the best way to refresh your data is creating a new context. – Gert Arnold Sep 22 '15 at 07:32
  • Mentioned that in my post... do you have a solution for cases where one needs to keep data without adding a whole new layer of bookkeeping? – Mårten Sep 22 '15 at 11:55
  • I just had an interesting idea which I will investigate. Would it be wise to keep one context for changes and one for showing information (which might be changed?), as long as they aren't mixed up? If I'm able to produce something good, I'll post it. – Mårten Sep 22 '15 at 11:58
  • https://stackoverflow.com/questions/9081244/reload-an-entity-and-all-navigation-property-association-dbset-entity-framework use `context.Entry(myPerson).Collection(p => p.Addresses).CurrentValue.Clear();` and `context.Entry(myPerson).Collection(p => p.Addresses).Load();` – Jinjinov Aug 29 '19 at 13:14

0 Answers0