I have simple ObjectListView
which displays some columns from EntityFramework
of objects called Person
(to simplify things). I load it with Entity Framework to ObjectListview
and then deatch
it from Context. The person has multiple fields (like Name, Surname, File, ...).
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 ObjectListView
the selected Person
is loaded into TextBox fields. When editing any of the fields I check if Person
Name or SurName changed and if it did I updated the context like in the code below.
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();
}
}
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 SAVE ALL
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.
So what are my options? I was pointed to use INotifyPropertyChanged 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.
The other option was to actually create bool value per each Column
from the database like
public partial class Person
{
public bool NameChanged {get;set; }
public bool SurnameChanged { get; set; }
}
And when saving I would be checking if bool changed and if yes do the ObjectStateManager magic on the context prior to save.
Any other way to do this?