如何在大多数时间轻松过滤EF DbSet? [英] How can I easily filter an EF DbSet most of the time?

查看:157
本文介绍了如何在大多数时间轻松过滤EF DbSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个建筑物的集合。到目前为止,可以说,我需要跟踪的所有建筑物都处于活动状态。但是,现在需要包括已被拆除并且不再存在的建筑物。在 Building 类中添加一个名为 Decommissioned 的可为空的DateTime属性似乎很明智,以跟踪建筑物的建造时间

I have a collection of Buildings in my database. Up to this point, all buildings I've needed to track have been active, so to speak. A need has now arisen, though, to include a building that has been demolished and no longer exists. It seems fairly sensible to add a nullable DateTime property called Decommissioned to the Building class to track when a building has been taken offline.

我的大多数应用程序都是通过从DbContext中获取所有建筑物的全部集合来获取建筑物的。向每个实例添加额外的 dbcontext.Buildings.Where(b => b.Decommissioned == null)会很痛苦,因此要在IDbSet级别进行过滤会很好,但是我仍然需要这些退役的建筑物在某些情况下显示出来,例如取消居民的租约历史。

The vast majority of my application fetches buildings by simply getting the entire set of all buildings from the DbContext. It would be a pain to add an additional dbcontext.Buildings.Where(b => b.Decommissioned == null) check to every instance, so filtering at the IDbSet level would be nice, but I still need these decommissioned buildings to show up in some contexts, like pulling up a resident's history of tenancies.

问题在很大程度上归结为,如何我能否拥有 dbContext.Buildings 的默认行为,以便让我仅获得未退役的建筑物,但仍以特殊方式要求数据库专门 include 当我确实想要建筑物时退役了吗?

The question largely boils down to, how can I have the default behavior of dbContext.Buildings be to get me only non-decommissioned buildings, but still ask the DB in a special way to specifically include decommissioned buildings when I do want them?

推荐答案

经过几天的探索,我无法完全解决做了我想做的事,最终我发现 EntityFramework.DynamicFilters包确实可以完成我的工作需要,实际上已经打开了一堆新的机会能力。

After several days of exploring possible solutions that I couldn't quite get to do what I wanted, I ultimately found the EntityFramework.DynamicFilters package that did exactly what I needed and has in fact opened up the door to a bunch of new possibilities.

这篇关于如何在大多数时间轻松过滤EF DbSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆