如何使用 WPF DataGrid 和实体框架进行软删除?

发布于 2025-01-05 19:30:18 字数 440 浏览 4 评论 0原文

假设我有下表:

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 技术交流群。

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

发布评论

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

评论(1

云雾 2025-01-12 19:30:18

您没有提供有关如何使用数据网格的大量信息,因此我必须做出一些假设。

如果您希望当用户从数据网格中删除行时发生“软删除”,您需要启用行删除。您可以通过将 CanUserDeleteRows 设置为 True 来实现此目的。

我假设您正在使用某种数据绑定来绑定数据网格中的行。当删除一行时,ItemsSource 中的基础对象将从该集合中删除。如果您使用实现 INotifyCollectionChanged 的集合(例如 ObservableCollection)发生这种情况时会触发一个事件。

您可以侦听该事件并通过将 DeletionDate 设置为 DateTime.Now 来相应地修改底层模型对象。然后,您必须调用 entities.SaveChanges() 将该更改推送到数据库。

private void ReloadData()
{
    var viewSource = (CollectionViewSource)this.FindResource("aTableViewSource");
    var aTableRows = new ObservableCollection<aTable>
    (
        this.entities.aTable
            .Where(fibra => !fibra.DELETIONDATE.HasValue)
            .Take(10)
            .ToList()
    );

    fibras.CollectionChanged += EventHandler;
    fibrasViewSource.Source = aTableRows;
}

void EventHandler(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Remove)
    {
        foreach(var oldItem in e.OldItems)
        {
            ((aTable)oldItem).DELETIONDATE = DateTime.Now;
        }
    }
}

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 to True.

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 implementing INotifyCollectionChanged (like ObservableCollection) an event is fired when that happens.

You can listen to that event and modify the underlying model object accordingly by setting DeletionDate to DateTime.Now. You will then have to call entities.SaveChanges() to push that change to the database.

private void ReloadData()
{
    var viewSource = (CollectionViewSource)this.FindResource("aTableViewSource");
    var aTableRows = new ObservableCollection<aTable>
    (
        this.entities.aTable
            .Where(fibra => !fibra.DELETIONDATE.HasValue)
            .Take(10)
            .ToList()
    );

    fibras.CollectionChanged += EventHandler;
    fibrasViewSource.Source = aTableRows;
}

void EventHandler(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Remove)
    {
        foreach(var oldItem in e.OldItems)
        {
            ((aTable)oldItem).DELETIONDATE = DateTime.Now;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文