Entity framework &引用;软删除";在实体框架5中

Entity framework &引用;软删除";在实体框架5中,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,我有一些使用EF5在应用程序中软删除记录的代码。我的“deletable”类实现了ISoftDelete,它只是说实现者必须具有bool Deleted属性 当我的用户单击删除时,我调用DbContext..Remove(实体) 这会将绑定到父实体的所有属性清除为null(如果父实体具有可删除实体的集合!) 在我的DbContext中,我重写SaveChanges方法以查找任何已删除的实体,如果它们实现了我的ISoftDelete接口,我将状态设置为modified而不是deleted,并将其d

我有一些使用EF5在应用程序中软删除记录的代码。我的“deletable”类实现了ISoftDelete,它只是说实现者必须具有bool Deleted属性

当我的用户单击删除时,我调用DbContext..Remove(实体)

这会将绑定到父实体的所有属性清除为null(如果父实体具有可删除实体的集合!)

在我的DbContext中,我重写SaveChanges方法以查找任何已删除的实体,如果它们实现了我的ISoftDelete接口,我将状态设置为modified而不是deleted,并将其deleted属性设置为true以标记为deleted。我的问题是,包含对父对象引用的属性是空的


调查似乎指向ApplyOriginalValues,但由于我的价值观不是公共财产,而是由于我是集合中的一个孩子而为我创建的,所以我很难实现。你能帮忙吗?

我想如果你用另一种方法可能会更容易。 使用存储库和工作单元fascade模式而不是直接调用EF上下文,意味着您可以更轻松地控制操作。 工作类的单位控制savechanges操作。 repository类控制CRUD、GetList等

   public class RepositoryBase<TPoco> : IRespository {

       public RepositoryBase(DbContext context) { Context = context; }
          //... CRUD methods....
          public bool Remove(TPoco poco) {
                   if (typeof ISoftDelete).IsAssignableFrom(Typeof(TPoco)){
                       // proceed with modify actions
                   }
                   else {
                          Context.Set<TPoco>().Remove(poco);
                   }
          }
   }

   public class Luw : ILuw{
         // ....
          public IRepositoryBase<TPoco> GetRepository<TPoco>() where TPoco : ???{
              return (new RepositoryFactory<TPoco>().GetRepository(Context));
         }

  public MuCustomResult  Commit() {
          .... any special context manipulation before save
          myCustomResultRecordsAffected = Context.SaveChanges();

  }
}
public类RepositoryBase:IRespository{
公共RepositoryBase(DbContext context){context=context;}
//…积垢方法。。。。
公共厕所拆除(TPoco poco){
if(ISoftDelete的类型)。IsAssignableFrom(TPoco的类型)){
//继续修改操作
}
否则{
Context.Set().Remove(poco);
}
}
}
公共类Luw:ILuw{
// ....
公共IRepositoryBase GetRepository(),其中TPoco:{
返回(newrepositoryfactory().GetRepository(Context));
}
公共MuCustomResult提交(){
..保存前的任何特殊上下文操作
myCustomResultRecordsAffected=Context.SaveChanges();
}
}

菲尔,我想你可能是对的。不知道需要付出多少努力,但这绝对是正确的方法。谢谢你的建议,我将在互联网上搜索一个简单的EF存储库示例/教程。如果你可以访问pluralsight,我知道这里有一些很好的示例。但加入要付出代价。