1

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.

1
  • Can you post example of what and how exactly you want to achieve this? Commented Aug 30, 2011 at 12:28

2 Answers 2

2

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;
        }
    }  
}
Sign up to request clarification or add additional context in comments.

2 Comments

So the combination of EF and RIA Services has nothing resembling the QueryInterceptors in WCF Data Services?
I'm not sure about RIA services but EF itself doesn't have any such functionality. QueryInterceptors are specific to WCF Data Services.
1

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>().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.