本文介绍了导航属性为软删除实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个的entites:
即时我的数据库,他们是这样的:

I have 2 entites:
Im my DB they look like:

Vehicles(Id, VehicleNumber, IsDeleted, WorkerId)
Workers(Id, Name, Address)

在我的EDMX:

And in my edmx:

VehicleId: Id, VehicleNumber, IsDeleted, WorkerId, Worker
Workers: Id, Name, Address, VehiclesList

正如你所看到的,车辆表中包含软删除的行。现在,当我得到工人的ID = 2,我得到了他的所有车辆,包括我软删除。我怎么能只检索未删除的车辆?

As you can see, Vehicles table contains soft deleted rows. Now when I get Worker with id=2, I got all his vehicles, including the one I soft deleted. How can I retrieve only the undeleted vehicles?

推荐答案

厉害。 EF具有软删除的支持非常有限。其实,唯一的可能就是使用条件映射在那里你明确地硬code(不能在运行时改变),以你映射的条件说,你不希望加载有实体请将isDeleted = 0 。检查映射的详细信息:

Badly. EF has very limited support for soft deletes. Actually the only possibility is using conditional mapping where you explicitly hardcode (it cannot be changed at runtime) to your mapping condition saying that you don't want to load entities having IsDeleted = 0. Check mapping details:

但它有非常不好的后果:

But it has very bad consequences:

  • 请将isDeleted 列不能映射 - 它已在内部定义映射
  • 您的模型不能使用,即使你要加载软删除实体
  • IsDeleted column cannot be mapped - it already defines mapping internally
  • Your model can never be used to load soft deleted entities even if you want

的第一个问题可以通过映射存储过程来解决,以删除操作的车辆实体和第二个问题可以通过为审计单独的模型和检索删除的实体来解决。

The first problem can be solved by mapping stored procedure to delete operation for the Vehicle entity and the second problem can be solved by separate model for auditing and retrieving deleted entities.

也是有条件的映射不支持code第一 - 它需要EDMX文件

Also conditional mapping is not supported by code first - it requires EDMX file.

这篇关于导航属性为软删除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:28