Is it possible to add a sort-of a global filter to an Entity Framework object context? Such as having an ObjectMaterialized which can return an indicator of whether or not to include a given object in the result set.
2 Answers
No it is not possible. Entity framework and its built in providers don't have any support for global filters.
You can achieve some basic filtering with simple wrapper:
public class MyContext : ObjectContext
{
private ObjectSet<MyEntity> myEntities;
public Expression<Func<MyEntity, bool>> GlobalMyEntityFilter { get; set; }
public IQueryable<MyEntity> MyEntities
{
get
{
if (GlobalMyEntityFilter != null)
{
return myEntities.Where(GlobalMyEntityFilter);
}
return myEntities;
}
}
}
2 Comments
BowlingForGreens
So the combination of EF and RIA Services has nothing resembling the QueryInterceptors in WCF Data Services?
Ladislav Mrnka
I'm not sure about RIA services but EF itself doesn't have any such functionality. QueryInterceptors are specific to WCF Data Services.
Are you trying to do something like only show the active Customers? If so, you can use Inheritance and create an ActiveCustomer type and add a condition in your mapping to Status == "Active". Then set your Customer type as an Abstract Base Class to prevent direct instantiation. You can then query your model for Customers.OfType<ActiveCustomer>().