在wpf中的新窗口中编辑实体
我有一个显示实体列表的窗口,我想在新窗口中编辑 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于实体框架来说,远离数据上下文中的“使用”是一个非常糟糕的方法!
如果您在保存之前关闭数据上下文,则所有实体结果都会断开连接并保存为无结果。
尝试这种方式,使用类级别上下文,保持连接并使用实体框架的所有功能
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
为什么使用 View.EditItem、View.CommitEdit 和 View.CancelEdit?您所需要的只是 win.DataContext = SelectedEmployee。我不明白的是,当您将新编辑的数据设置到您的实体时?
您从数据库获取员工,但不将 SelectedEmployee 中编辑的数据应用到您的员工。或者我错过了什么?
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?
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
so why you dont use it and save it back to db?
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