Note that there are some explanatory texts on larger screens.

plurals
  1. PONotifying Entity Framework context about changes at later time rather then at object's properties change
    primarykey
    data
    text
    <p>I have simple <code>ObjectListView</code> which displays some columns from <code>EntityFramework</code> of objects called <code>Person</code> (to simplify things). I load it with Entity Framework to <code>ObjectListview</code>and then <code>deatch</code> it from Context. The person has multiple fields (like Name, Surname, File, ...). </p> <p>Considering that some files are 5mb-50mb I don't want to load/save them to database without need. Now when someone double clicks in <code>ObjectListView</code> the selected <code>Person</code> is loaded into TextBox fields. When editing any of the fields I check if <code>Person</code> Name or SurName changed and if it did I updated the context like in the code below.</p> <pre><code> private void test (Person person){ using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) { context.Persons.Attach(dokument); if (person.Name != nameTextBox.Text) { person.Name = nameTextBox.Text; context.ObjectStateManager.GetObjectStateEntry(person).SetModifiedProperty("Name"); } if (person.SurName != surNameTextBox.Text) { person.SurName = surNameTextBox.Text; context.ObjectStateManager.GetObjectStateEntry(person).SetModifiedProperty("SurName"); } context.SaveChanges(); } } </code></pre> <p>This works.. but what I would like to do is actually not save the changes straight away after users changes it but in the end when he presses BIG button <code>SAVE ALL</code> so then it would go thru all ObjectListView items and update them as needed in database. The problem is I can't update context and do it later since the Context wouldn't be used then but in the end when saving everything. </p> <p>So what are my options? I was pointed to use <a href="https://stackoverflow.com/questions/9104553/how-to-find-out-if-an-entity-framework-object-has-changed">INotifyPropertyChanged</a> but as much as I looked at the code I don't think it's good for this type of situation or I simply don't know how to use it.</p> <p>The other option was to actually create bool value per each <code>Column</code> from the database like</p> <pre><code>public partial class Person { public bool NameChanged {get;set; } public bool SurnameChanged { get; set; } } </code></pre> <p>And when saving I would be checking if bool changed and if yes do the ObjectStateManager magic on the context prior to save. </p> <p>Any other way to do this? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload