如何使用EntityFramework基于IsDeleted = true加载根对象及其子实体 [英] How to load a root object and it's child entities based on IsDeleted = true using EntityFramework

查看:878
本文介绍了如何使用EntityFramework基于IsDeleted = true加载根对象及其子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用授权框架实现软删除。

I have to implement soft delete using entitty framework.


  • 我的数据库表有一列 IsDeleted

  • 对应的实体也有 IsDeleted 字段。

  • 当用户删除子实体时, IsDeleted 为该子对象设置为true并获得持久化。

  • 在加载实体对象图时,我想确保所有相关实体都加载了一个条件 IsDeleted = false。

  • 尝试使用以下链接的帮助,但我收到很多错误: handling-logical-delete-with-entity-framework-4

  • 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 :


错误3032:从第38行开始:条件成员具有非IsNull = False的条件被映射。请移除EntityHavingClause.IsDeleted上的条件或从映射中移除它。

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.

问:任何人都可以告诉我在哪里出错,加载 IsDelete = false的所有子属性的最简单方法是什么?

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?

推荐答案

您将 EntityHavingClause.IsDeleted 映射为属性作为条件。

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.

编辑:

由于您需要这两个属性和条件,可能最好的方法是将计算列添加到数据库表,只显示IsDeleted的值(如果可以更改模式,也就是)。然后根据计算的列映射条件。

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.

我认为这比尝试使用EF来映射同一列两次(如果可以做到)更容易。

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

这篇关于如何使用EntityFramework基于IsDeleted = true加载根对象及其子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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