ExpressionVisitor soft delete

后端 未结 2 360
梦毁少年i
梦毁少年i 2020-12-10 22:09

We\'re having some issues implementing soft delete functionality with entity framework. The idea is to use a repository which is aware of the EF context. On the level of the

相关标签:
2条回答
  • 2020-12-10 22:38

    Yes, using ExpressionVisitor is the correct approach.

    You need to transform x.Bonus into x.Bonus.Where(x => !x.IsDeleted). I suggest you do the same thing for x.Bonus.Where(y => y.bonID == 100). Transform it into x.Bonus.Where(x => !x.IsDeleted).Where(y => y.bonID == 100)

    This means that you need to convert any Expression of type IQueryable<Bonus> to another expression of type IQueryable<Bonus>, but with the where-clause appended.

    You probably need to override the very general ExpressionVisitor.Visit method to visit all expressions, not just binary ones.

    You are very likely to run into special cases here that you haven't thought about yet. This is going to be difficult, but fun :)

    0 讨论(0)
  • 2020-12-10 22:41

    Victory! Today I've created an ExpressionVisitor which appends IsDeleted where clause to each select, even in navigation properties!

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