16358

How can I easily filter an EF DbSet most of the time?

Question:

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?

Answer1:

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.

Recommend

  • How do I imply the type of the value when there are no type parameters or ascriptions?
  • Fetch all binded events using jquery
  • Several images on one UIButton
  • In Rails, is it possible to use the show view as an edit view?
  • Drawing multiple triangles in OpenGL
  • Google Maps: Norwegian postcodes not returning any results
  • combination of smote and undersampling on weka
  • Maven: Command line to download the dependencies described in the pom.xml
  • Cluster markers with osmdroid
  • How can I associate a custom URL protocol with a ClickOnce app?
  • finding greatest prime factor using recursion in c
  • How to clip image to ellipse in XAML
  • Error building image: Error building rlang - not available on R 3.5.0 (Uploading shiny)
  • cordova build android throws error on Ubuntu 12.04
  • How to use SBT with multiple sub project web applications?
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • Calling C function from lua
  • Fat binaries in iOS
  • Using SWIG with a build system [closed]
  • Efficient algorithm to find additions and removals from 2 collections
  • Building Qt project for C++11 standard
  • Rails Find when some params will be blank
  • Database structure design with variable amounts of fields
  • Google Custom Search with transparent background
  • Spring Data JPA custom method causing PropertyReferenceException
  • Jenkins: How To Build multiple projects from a TFS repository?
  • C# - Serializing and deserializing static member
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Sending data from AppleScript to FileMaker records
  • Why winpcap requires both .lib and .dll to run?
  • How to include full .NET prerequisite for Wix Burn installer
  • How to set the response of a form post action to a iframe source?
  • Java static initializers and reflection
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app