0

In my application I manage persons information, I have a listBox display list of persons and three buttons Add/Edit/Delete, When I select row and press edit button the application open a new window with person information and save to some database table (ModifyCheck) that this person modifying now if other user try to edit it. In this person window I have two button (Save/Cancel). all controls in this window binding with object property, When I make some modifying and press save I should remove the row from (ModifyCheck).

The Problem Is :

If I open the person window and modifying some information then press Cancel, I will update ModifyCheck table by using SaveChange. In this case all the information that I modifyting will updated although I pressed Cancel.

How Can I apply SaveChanges() for ModifyCheck only ?

Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66

1 Answers1

1

Do not bind your entity properties directly to controls. Create a view model with the same properties, bind them to controls. When user makes some changes, it will modify viewModel's properties, instead of your entity. In case when user presses Save button, you have to update entity's fields. If user presses Cancel, nothing will happen to entity.

For simplicity, imagine you have a WinForms application. Every TextBox control has a name. When user presses Save button, you will assign it's value to entity:

person.FirstName = textBoxFirstName.Text;
db.SaveChanges();

Otherwise you will close the form and nothing will change person

opewix
  • 4,993
  • 1
  • 20
  • 42
  • Thank for your answer, Please could you guide me how to use view model in detail? – Abdulsalam Elsharif Jul 16 '17 at 18:04
  • You can't just use ViewModel if you are not aware of MVVM pattern and how it's used in WPF. Here is related topic, MVVP tutorial: https://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish – opewix Jul 16 '17 at 18:09
  • @AbdulsalamElsharif MVVM may be too complex topic here. You have to understand only one thing, do not modify entity fields until user presses `Save` button. – opewix Jul 16 '17 at 18:10