软件删除(IsHistorical列)与EntityFramework [英] Soft Deletes ( IsHistorical column ) with EntityFramework

查看:122
本文介绍了软件删除(IsHistorical列)与EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据库,设计师决定用IsHistorical位列标记每个表格。没有考虑到正确的建模,没有办法我可以改变模式。



当开发与导航属性交互的CRUD屏幕时,这将导致一些摩擦。我不能简单地拿一个产品,然后编辑它的EntityCollection我必须手动写IsHistorical检查遍及整个地方,并驱使我疯狂。



添加也是可怕的,因为到目前为止我已经编写了所有的手动检查,以查看添加是否只是软删除,而不是添加一个重复的实体,我可以切换IsHistoric。



我有三个选项考虑的是:


  1. 修改t4模板以包括IsHistorical检查和同步。


  2. 在ObjectContext中截取删除和添加,切换IsHistorical列,然后同步对象状态。


  3. 订阅AssociationChanged事件并在其中切换IsHistorical列。


有人有任何经验或者可以推荐最无痛的方法吗? / p>

注意:是的,我知道这是糟糕的建模。我已经阅读了有关您所拥有的软删除的相同文章。它发臭,我必须处理这个要求,但我这样做。我只想要处理软删除的最无痛苦的方法,而不为我的数据库中的每个导航属性编写相同的代码。



注意#2 LukeLed的答案在技术上是正确的,尽管力量你成为一个非常糟糕的穷人的ORM,无图形,模式。问题在于现在我需要从图中删除所有的已删除对象,然后调用每个对象的Delete方法。这不是真的会救我这么多手工礼仪编码。而不是现在编写手册IsHistoric检查,我正在收集删除的对象并循环使用它们。

解决方案

重新发现,当您无法修改模式时,不会有一个很好的解决方案。鉴于您不喜欢Repository选项(尽管如此,我不知道您是否不太会仓促地将其解除),以下是我能想到的最好的方法:


  1. 处理 ObjectContext.SavingChanges

  2. 当该事件触发时,拖动 ObjectStateManager 查找已删除状态的对象。如果他们有一个 IsHistorical 属性,请设置,并将对象的状态更改为修改。

在协会/关系方面,这可能会变得棘手,但我认为它或多或少是你想要的。


I'm working with a database where the designers decided to mark every table with a IsHistorical bit column. There is no consideration for proper modeling and there is no way I can change the schema.

This is causing some friction when developing CRUD screens that interact with navigation properties. I cannot simply take a Product and then edit its EntityCollection I have to manually write IsHistorical checks all over the place and its driving me mad.

Additions are also horrible because so far I've written all manual checks to see if an addition is just soft deleted so instead of adding a duplicate entity I can just toggle IsHistoric.

The three options I've considered are:

  1. Modifying the t4 templates to include IsHistorical checks and synchronization.

  2. Intercept deletions and additions in the ObjectContext, toggle the IsHistorical column, and then synch the object state.

  3. Subscribe to the AssociationChanged event and toggle the IsHistorical column there.

Does anybody have any experience with this or could recommend the most painless approach?

Note: Yes, I know, this is bad modeling. I've read the same articles about soft deletes that you have. It stinks I have to deal with this requirement but I do. I just want the most painless method of dealing with soft deletes without writing the same code for every navigation property in my database.

Note #2 LukeLed's answer is technically correct although forces you into a really bad poor mans ORM, graph-less, pattern. The problem lies in the fact that now I'm required to rip out all the "deleted" objects from the graph and then call the Delete method over each one. Thats not really going to save me that much manual ceremonial coding. Instead of writing manual IsHistoric checks now I'm gathering deleted objects and looping through them.

解决方案

As I'm sure you're aware, there is not going to be a great solution to this problem when you cannot modify the schema. Given that you don't like the Repository option (though, I wonder if you're not being just a bit hasty to dismiss it), here's the best I can come up with:

  1. Handle ObjectContext.SavingChanges
  2. When that event fires, trawl through the ObjectStateManager looking for objects in the deleted state. If they have an IsHistorical property, set that, and changed the state of the object to modified.

This could get tricky when it comes to associations/relationships, but I think it more or less does what you want.

这篇关于软件删除(IsHistorical列)与EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆