如何使用 WPF DataGrid 和实体框架进行软删除?
假设我有下表:
CREATE TABLE EXAMPLETABLE (
ID NUMBER(10,0) NOT NULL,
DELETIONDATE DATE,
NAME VARCHAR2(100 CHAR) NOT NULL,
UNIQUE (DELETIONDATE, NAME));
如何从 WPF DataGrid
中软删除项目?软删除是下面的操作:
UPDATE EXAMPLETABLE
SET DELETIONDATE = NOW()
WHERE ID = SOMEID;
我应该处理KeyPress事件吗? 在表单中我有一个“保存”按钮,我应该在调用 entities.SaveChanges()
之前做一些处理吗?
Suppose I have the following table:
CREATE TABLE EXAMPLETABLE (
ID NUMBER(10,0) NOT NULL,
DELETIONDATE DATE,
NAME VARCHAR2(100 CHAR) NOT NULL,
UNIQUE (DELETIONDATE, NAME));
How to soft delete an item from a WPF DataGrid
? Soft deletion is the operation below:
UPDATE EXAMPLETABLE
SET DELETIONDATE = NOW()
WHERE ID = SOMEID;
Should I handle the KeyPress event?
In the form I have a Save button, should I do some treatment before calling entities.SaveChanges()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供有关如何使用数据网格的大量信息,因此我必须做出一些假设。
如果您希望当用户从数据网格中删除行时发生“软删除”,您需要启用行删除。您可以通过将
CanUserDeleteRows
设置为True
来实现此目的。我假设您正在使用某种数据绑定来绑定数据网格中的行。当删除一行时,
ItemsSource
中的基础对象将从该集合中删除。如果您使用实现INotifyCollectionChanged
的集合(例如ObservableCollection
)发生这种情况时会触发一个事件。您可以侦听该事件并通过将
DeletionDate
设置为DateTime.Now
来相应地修改底层模型对象。然后,您必须调用entities.SaveChanges()
将该更改推送到数据库。You havn't provided a lot of information about how you use the datagrid so I will have to make some assumptions.
If you want the "soft deletion" to happen when the user deletes a row from the datagrid you need to enable deletion of rows. You do that by setting
CanUserDeleteRows
toTrue
.I assume that you are using some kind of databinding to bind the rows in the datagrid. When a row is deleted the underlying object in
ItemsSource
is removed from that collection. If you use a collection implementingINotifyCollectionChanged
(likeObservableCollection
) an event is fired when that happens.You can listen to that event and modify the underlying model object accordingly by setting
DeletionDate
toDateTime.Now
. You will then have to callentities.SaveChanges()
to push that change to the database.