Mohammad Zare
Mohammad Zare

Reputation: 1509

Edit an entity in a new window in wpf

i have a window that shows a list of entities and i want to edit the selecteitem of gridview in a new window (Not in grid). when i submit my form no error occurred but entity have no changes in database! please help me.

in top of my list window code behind:

            private ObservableCollection<Employee> AllEmployeesData { get; set; }
            private ListCollectionView View;

and in window_loaded i use this method for fetch data:

public void LoadAllEmployees()
    {
        IEnumerable<Employee> data = null;
        using (ArchiveEntities db = new ArchiveEntities())
        {
            data = db.Employees.Include("Department");
            this.AllEmployeesData = new ObservableCollection<Employee>(data);
        }
        CollectionViewSource employeeSource = (CollectionViewSource)this.FindResource("AllEmployeesDataSource");
        employeeSource.Source = this.AllEmployeesData;
        this.View = (ListCollectionView)employeeSource.View;
    }

Editbutton click event:

            EditEmployeeView win = new EditEmployeeView();
            View.EditItem(SelectedEmployee);
            win.DataContext = SelectedEmployee;

            if ((bool)win.ShowDialog())
            {
                using (ArchiveEntities db = new ArchiveEntities())
                {
                    Employee employee = db.Employees.Single(x => x.Id == SelectedEmployee.Id);
                    db.Employees.ApplyCurrentValues(employee);
                    db.SaveChanges();
                    View.CommitEdit();
                }
            }
            else
            {
                View.CancelEdit();
            }

all of the above code is in my first window (window that shows a list of entities). and in my second window (window for edit selected item of a first window):

submitbutton click event:

        DialogResult = true;
        Close();

my problem is: when i submit edit form no error occurred but data dont save in database and when i cancel edit form i get this error:

InvalidOperationException was unhandled: CancelEdit is not supported for the current edit item.

Upvotes: 0

Views: 3221

Answers (3)

Xavave
Xavave

Reputation: 695

Employee class must implement IEditableObject you can see an example here : https://msdn.microsoft.com/en-us/library/system.componentmodel.ieditableobject.aspx After this implementation, it should work as expected

Upvotes: 0

blindmeis
blindmeis

Reputation: 22445

why you use View.EditItem,View.CommitEdit and View.CancelEdit? all you need is your win.DataContext = SelectedEmployee. what i dont get is when you set your new edited data to your entity?

using (ArchiveEntities db = new ArchiveEntities())
{
    Employee employee = db.Employees.Single(x => x.Id == SelectedEmployee.Id);
    db.Employees.ApplyCurrentValues(employee);
    db.SaveChanges();
    View.CommitEdit();
 }

you get the employee from db but you dont apply the edited data from SelectedEmployee to your employee. or do i miss something?

the SelectedEmployee is a entity from your db

data = db.Employees.Include("Department");
this.AllEmployeesData = new ObservableCollection<Employee>(data);

so why you dont use it and save it back to db?

db.SaveChanges(SelectedEmployee );

Upvotes: 1

Xilmiki
Xilmiki

Reputation: 1502

Go away from "using" in datacontext is a really bad approach for entity framework!

If you close your datacontext before save, all entity result disconnected and save as no resut.

Try this way, use a class level context, stay connected and use all power of entityframework

public mainClass{

  private ArchiveEntities db;
  private ObservableCollection<Employee> allEmployeesData;
  private Employee selctedEmplyee;

  // property in binding
   public ObservableCollection<Employee> AllEmployeesData { get{return allEmployeesData;} set{allEmployeesData=value; onPropertyChanged("AllEmployeesData"); }
   public Employee SelctedEmplyee { get{return selctedEmplyee;} set{selctedEmplyee=value; onPropertyChanged("SelctedEmplyee"); }

  mainWindow (){ //Constructor

  db=new ArchiveEntities();
  }

  private void onedit(){
     new detailWindow(SelectedEmployee).ShowDialog();
     //reload from db, upadte current element  if modified in the detail window
     SelectedEmployee = db.Employees.Single(x => x.Id == SelectedEmployee.Id);
  }

  //no need to save in main window (is only for view)

}

public class detailWindow(){

  private ArchiveEntities db;
  private Employee selctedEmplyee;

  //employee to modify
  public Employee SelctedEmplyee { get{return selctedEmplyee;} set{selctedEmplyee=value; onPropertyChanged("SelctedEmplyee"); }


  public detailWindow(Employee SelectedEmployee){

    db=new ArchiveEntities; // a new indipendent context
    SelectedEmployee = db.Employees.Single(x => x.Id == SelectedEmployee.Id);
  }

  public void onSave(){
    db.SaveChanges(); //effect only in SelectedEmployee
    // if you don'save main window data will not change
  }


}

Upvotes: 1

Related Questions