软删除实体框架$ C C首先$实体、框架

2023-09-02 10:17:02 作者:閉上眼說再見

所以我使用实体框架$ C C首先$

So I am using Entity Framework Code First

我重写的SaveChanges 的DbContext 来让我做一个软删除

I override SaveChanges in DbContext to allow me to do a "soft delete"

                        if (item.State == EntityState.Deleted && 
typeof(ISoftDelete).IsAssignableFrom(type))

                    {
                        item.State = EntityState.Modified;
                        item.Entity.GetType().GetMethod("Delete")
                         .Invoke(item.Entity,null);

                        continue;
                    }

这是伟大的,让对象知道如何标记本身作为一个软删除(在这种情况下,它只是请将isDeleted设置为true)

Which is great, so the object knows how to mark itself as a soft delete (In this case it just sets IsDeleted to true)

我的问题是我怎么可以把它这样,当我找回它忽略任何请将isDeleted与对象?所以,如果我说 _db.Users.FirstOrDefault(用户ID = = ID)如果该用户有 ==请将isDeleted真正它会忽略它。基本上我想过滤?

My question is how can I make it such that when I retrieve the object it ignores any with IsDeleted ? So if I said _db.Users.FirstOrDefault(UserId == id) if that user had IsDeleted == true it would ignore it. Essentially I want to filter?

注:我不想只是把和放大器;&安培; ==请将isDeleted真 这就是为什么我打了类与接口,这样的删除知道如何只是工作,我想以某种方式修改检索要懂得只是工作也是基于该接口是present。

Note: I do not want to just put && IsDeleted == true That's why I am marking the classes with an interface so the remove knows how to "Just Work" and I'd like to somehow modify the retrieval to know how to "Just Work" also based on that interface being present.

在此先感谢

推荐答案

我有软删除的工作为我所有的实体和软删除的项目不通过利用的