在wpf中的新窗口中编辑实体

发布于 2024-12-20 22:30:49 字数 1884 浏览 1 评论 0原文

我有一个显示实体列表的窗口,我想在新窗口中编辑 gridview 的选择项(不在网格中)。当我提交表单时没有发生错误,但实体在数据库中没有更改!请帮我。

在我的列表窗口顶部的代码后面:

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

在 window_loaded 中,我使用此方法来获取数据:

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 单击事件:

            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();
            }

以上所有代码都在我的第一个窗口中(显示实体列表的窗口)。 在我的第二个窗口(用于编辑第一个窗口的所选项目的窗口)中:

submitbutton单击事件:

        DialogResult = true;
        Close();

我的问题是:当我提交编辑表单时没有发生错误,但数据没有保存在数据库中,当我取消编辑表单时,我收到此错误:

InvalidOperationException 未处理:不支持 CancelEdit 对于当前编辑项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2024-12-27 22:30:50

对于实体框架来说,远离数据上下文中的“使用”是一个非常糟糕的方法!

如果您在保存之前关闭数据上下文,则所有实体结果都会断开连接并保存为无结果。

尝试这种方式,使用类级别上下文,保持连接并使用实体框架的所有功能

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
  }


}

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
  }


}
嘿嘿嘿 2024-12-27 22:30:50

为什么使用 View.EditItem、View.CommitEdit 和 View.CancelEdit?您所需要的只是 win.DataContext = SelectedEmployee。我不明白的是,当您将新编辑的数据设置到您的实体时?

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

您从数据库获取员工,但不将 SelectedEmployee 中编辑的数据应用到您的员工。或者我错过了什么?

SelectedEmployee 是数据库中的一个实体

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

,那么为什么您不使用它并将其保存回数据库呢?

db.SaveChanges(SelectedEmployee );

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 );
2024-12-27 22:30:50

Employee 类必须实现 IEditableObject
您可以在此处查看示例: https://msdn.microsoft .com/en-us/library/system.componentmodel.ieditableobject.aspx
实施后,它应该按预期工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文