How to load a root object and it's child entities based on IsDeleted = true using EntityFramework

孤者浪人 提交于 2019-12-11 04:43:48

问题


I have to implement soft delete using entitty framework.

  • My DB tables have a bit column IsDeleted.
  • Corresponding Entities also have IsDeleted field.
  • When a user deletes a child entity, IsDeleted in Set to true for that child object and gets persisted well.
  • While loading the entity-object graph, I want to ensure that all the relevant entities get loaded with a condition IsDeleted = false.
  • It tried using help from the following link, but I am getting lot of error : handling-logical-delete-with-entity-framework-4.

Here is the error I get for each entity :

error 3032: Problem in mapping fragments starting at line 38:Condition member with a condition other than 'IsNull=False' is mapped. Either remove the condition on EntityHavingClause.IsDeleted or remove it from the mapping.

Q: Can anyone please tell where I am going wrong and what would be the simplest way to load all child entitties whose IsDelete = false?


回答1:


You mapped EntityHavingClause.IsDeleted as a property and as a condition. You cannot do both.

When you follow the example you linked to, you should not need the property mapping.

Edit:

As you need both, the property and the condition, probably the best way to go is add a calculated column to your database table that just displays the value of IsDeleted (if you can change the schema, that is). Then map the condition against the calculated column.

I think that is easier than trying to hack EF to map the same column twice (if it can be done at all).




回答2:


This is not so easy. As @GertArnold pointed you cannot have colum mapped if you want to use it in condition and in the same time lazy and eager loading will not use your condition if it is not mapped. Each column can be mapped only once and condition is mapping.

The trick is that once you want soft delete you cannot expect that your business logic will handle it. It will become responsiblity of EF. You will need stored procedure mapped to delete operation of your entity which will set IsDeleted field instead of performing real delete. It is possible to map SQL command directly without using stored procedure but that is not supported in designer - you will have to maintain EDMX file manually as XML.

Your model will not support real deletion of the entity and when using this model you will never have access to soft deleted entities. That is how soft delete with entity framework works. If you need real delete or access to soft deleted entities you will need another model (another EDMX) where the entity will be mapped again without soft delete or you will have to use some trick to cheat first EDMX and map the entity again (database view or perhaps table alias).



来源:https://stackoverflow.com/questions/8991638/how-to-load-a-root-object-and-its-child-entities-based-on-isdeleted-true-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!