简体   繁体   中英

Soft delete on Parent Entity but Hard delete on Child/Relationship EF Core

I have 3 entities generated Code First in EF Core: User , Project and UserProject (many to many relationship). Up until now I used hard delete on User , which essentially cascaded into the relationship. Is there a way to implement soft deleting on User and hard deleting others without having to manually check for isDeleted flag in other relationship where this is referenced? Is there any way to trigger the OnDelete/OnUpdate with a custom action? Example: Define OnDelete as updating the isDeletedFlag, case in which I want to perform hard delete / set null on every other entity User is referenced in.

you can use ef core triggers. you can define triggers before save changes to your any specific entity with any approach. but it is available only in .net 5 and .net 6

https://github.com/koenbeuk/EntityFrameworkCore.Triggered

https://youtu.be/Gjys0Yebobk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM