当开发与导航属性交互的CRUD屏幕时,这将导致一些摩擦。我不能简单地拿一个产品,然后编辑它的EntityCollection我必须手动写IsHistorical检查遍及整个地方,并驱使我疯狂。
添加也是可怕的,因为到目前为止我已经编写了所有的手动检查,以查看添加是否只是软删除,而不是添加一个重复的实体,我可以切换IsHistoric。
我有三个选项考虑的是:
修改t4模板以包括IsHistorical检查和同步。
在ObjectContext中截取删除和添加,切换IsHistorical列,然后同步对象状态。
订阅AssociationChanged事件并在其中切换IsHistorical列。
有人有任何经验或者可以推荐最无痛的方法吗? / p>
注意:是的,我知道这是糟糕的建模。我已经阅读了有关您所拥有的软删除的相同文章。它发臭,我必须处理这个要求,但我这样做。我只想要处理软删除的最无痛苦的方法,而不为我的数据库中的每个导航属性编写相同的代码。
注意#2 LukeLed的答案在技术上是正确的,尽管力量你成为一个非常糟糕的穷人的ORM,无图形,模式。问题在于现在我需要从图中删除所有的已删除对象,然后调用每个对象的Delete方法。这不是真的会救我这么多手工礼仪编码。而不是现在编写手册IsHistoric检查,我正在收集删除的对象并循环使用它们。
解决方案重新发现,当您无法修改模式时,不会有一个很好的解决方案。鉴于您不喜欢Repository选项(尽管如此,我不知道您是否不太会仓促地将其解除),以下是我能想到的最好的方法:
在协会/关系方面,这可能会变得棘手,但我认为它或多或少是你想要的。
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:
Modifying the t4 templates to include IsHistorical checks and synchronization.
Intercept deletions and additions in the ObjectContext, toggle the IsHistorical column, and then synch the object state.
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:
This could get tricky when it comes to associations/relationships, but I think it more or less does what you want.
更多推荐
软件删除(IsHistorical列)与EntityFramework
发布评论