如何发送修改后的值来删除存储过程

时间:2021-07-08 22:10:11

I'm using Entity Framework 6 database first. The CUD operations of all my entities are mapped to stored procedures in the .edmx file. My DELETEs are not real deletes - they actually set a IsDeleted column from 0 to 1, and then the entities are filtered by IsDeleted = 0, so EF doesn't pick them up in SELECTs.

我首先使用Entity Framework 6数据库。我所有实体的CUD操作都映射到.edmx文件中的存储过程。我的DELETE不是真正的删除 - 它们实际上将IsDeleted列从0设置为1,然后通过IsDeleted = 0过滤实体,因此EF不会在SELECT中选择它们。

It all works well, except for one issue. My entities have a UpdateDate / UpdateBy column, which I want to update with the values of the current user and time when deleting. So I set them before calling .SaveChanges(), but what is sent to the database is the original values, and not the ones I've set.

这一切都运作良好,除了一个问题。我的实体有一个UpdateDate / UpdateBy列,我想用删除时的当前用户值和时间更新。所以我在调用.SaveChanges()之前设置它们,但发送到数据库的是原始值,而不是我设置的值。

It's worth noting that in order to delete, I pull the entity from the database (where these fields have some default values) and mark it for deletion.

值得注意的是,为了删除,我从数据库中提取实体(这些字段具有一些默认值)并将其标记为删除。

How do I make EF send the modified values to the DELETE stored procedure?

如何使EF将修改后的值发送到DELETE存储过程?

EDIT You wanted some code, so here it is. It's so trivial I thought it's redundant.

编辑你想要一些代码,所以在这里。这太微不足道了,我认为这是多余的。

public List<Audit> CreateAuditData(DbContext context)
{
    var entries = from e in context.ChangeTracker.Entries()
                  where e.State != EntityState.Unchanged
                  select e;
    foreach (var entry in entries)
    {
        Audit audit = new Audit();
        audit.CreateBy = audit.UserName = HttpContext.Current.User.Identity.Name;
        audit.CreateDate = DateTime.Now;

        IModel model = entry.Entity as IModel;
        switch (entry.State)
        {
        ...
        case EntityState.Deleted:
                model.UpdateBy = audit.CreateBy;
                model.UpdateDate = audit.CreateDate;
        ...
        break
        }
}

When I return from this method, I immediately call SaveChanges() on the context.

当我从这个方法返回时,我立即在上下文中调用SaveChanges()。

Audit is some other entity which I also save with every change. IModel is an interface all my entities implement, which contains the UpdateDate and UpdateBy properties.

审计是一些其他实体,我也会在每次更改时保存。 IModel是我所有实体实现的接口,它包含UpdateDate和UpdateBy属性。

1 个解决方案

#1


Since EF only sends the original values to the delete stored procedure, I had to modify the DbEntityEntry.OriginalValues collection of the entity with the values I wanted to send to the procedure. mapping the appropriate properties to procedure parameters, I was able to get these values in the database.

由于EF只将原始值发送到删除存储过程,因此我必须使用我想要发送给过程的值来修改实体的DbEntityEntry.OriginalValues集合。将适当的属性映射到过程参数,我能够在数据库中获取这些值。

#1


Since EF only sends the original values to the delete stored procedure, I had to modify the DbEntityEntry.OriginalValues collection of the entity with the values I wanted to send to the procedure. mapping the appropriate properties to procedure parameters, I was able to get these values in the database.

由于EF只将原始值发送到删除存储过程,因此我必须使用我想要发送给过程的值来修改实体的DbEntityEntry.OriginalValues集合。将适当的属性映射到过程参数,我能够在数据库中获取这些值。