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.
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.
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 <em>include</em> decommissioned buildings when I do want them?
After several days of exploring possible solutions that I couldn't quite get to do what I wanted, I ultimately found the <a href="https://www.nuget.org/packages/EntityFramework.DynamicFilters" rel="nofollow">EntityFramework.DynamicFilters package</a> that did exactly what I needed and has in fact opened up the door to a bunch of new possibilities.