Q:

Pre-filtering results with entity framework database-first

Using EF database-first, is it possible to create a duplicate of one of the classes, such that any query made comes back with an additional filter?
As an example: Given a class
 public partial class Person
 {
 public Person()
    {
        this.Job= new HashSet<Appointments>();
    }

    public int PersonID { get; set; }
    public int JobID { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }

  public virtual ICollection<Appointments> Appointments { get; set; }
  }

Is it possible to construct a duplicate of the class in some way that functions like the existing class, but will only return results applied a "where Forename = 'David')
I can't overwrite the existing class (both cases need to be kept, and it'll be overwritten anyway)
My first thought was to simply create a seperate static class with methods that return an IQueryable< Persons>, but to then call that later, the context has been disposed - I don't think you can attach it to a new context?

A:

The best you could do would be to add a function to your DbContext, in a partial class, that returns an IQueryable<Persons> with the filter already applied.  
The partial class should have the same name as your actual context class.  Any code in the partial class will be merged with the Database-First generated class, as if they were in the same file.  It also won't get touched or overwritten by the code-generator if you regenerate the context.  You can use this same concept to extend all kinds of code-generated classes (this is exactly the kind of use-case that partial classes were designed for).
public partial class MyDbContext
{
    public IQueryable<Persons> FilteredPersons()
    {
        return this.Persons.Where(p => p.Forename =="David");
    }
}

Then you can call it like this:
using (var myContext = new MyDbContext())
{
    var query = myContext.FilteredPersons().Where(...some additional filter...);
    var results = query.ToList();
}

You could probably also rig something up with an IDBCommandInterceptor, but that would be huge, hacky, ugly, and beyond the scope of a simple answer like this.

