making a global filter for entity framework

前端 未结 2 1319
星月不相逢
星月不相逢 2020-12-10 06:08

For my models I have a active attribute on all of them, and i want to filter all inactive if the model was not displayed on the administration What is the best

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 06:54

    You can do it like this:

    1. In OnModelCreating add an IsDeleted discriminator to every entity that can be soft deleted
    2. Override SaveChanges and find all the entries to be deleted
    3. Run SQL on these entries to set the IsDeleted discriminator then set their state to "detached"
    4. Change any unique indexes to ignore any soft deleted records

    You can find working code at this answer: How to soft delete using Entity Framework Code First

提交回复
热议问题