EF DbContext. How to avoid caching?

后端 未结 2 1171
清歌不尽
清歌不尽 2021-01-06 04:25

Spent a lot of time, but still cann\'t understand how to avoid caching in DbContext.

I attached below entity model of some easy case to demonstrate what I mean.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 05:12

    have you tried something like:

    ctx.Persons.Where(x => x.Flat.Building.Id == 1 && x.Archived == false);
    

    ===== EDIT =====

    In this case I think you approach is, imho, really hazardous. Indeed you works on the data loaded by EF to interpret your query rather than on data resulting of the interpretation of your query. If one day EF changes is loading policy (for example with a predictive pre-loading) your approach will "send you in then wall".

    For your goal, you will have to eager load the data you need to build your "filterd" entity. That is select the building, then foreach Flat select the non archived persons.

    Another solution is to use too separate contexts in an "UnitOfWork" like design.

提交回复
热议问题