navigation property to soft-deleted entity

前端 未结 2 627
悲&欢浪女
悲&欢浪女 2021-01-07 06:11

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

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

And in my edm

相关标签:
2条回答
  • 2021-01-07 06:43

    is lazyloading enabled? then try to limit result set with a where:

    worker.VehiclesList.Where(x=>!x.IsDeleted)
    

    also you can put a condition to a vehicles table mapping in model desiner isdeleted = false. Soft deleted vehicles will not be retrived at all

    0 讨论(0)
  • 2021-01-07 06:45

    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:

    enter image description here

    But it has very bad consequences:

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

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

    0 讨论(0)
提交回复
热议问题