In Entity Framework, how can we accomplish object filtering?

.net c# entity-framework


Using code first in entity framework while constructing an object context, for instance:

public class DomainContext : DbContext
    public DomainContext() { }
    public virtual DbSet<News> News { get; set; }

We all know that you may use the following query to search "News" (for instance, to obtain all news articles published today):

var ctx = new DomainContext();
ctx.News.Where(x => x.PublishedDate == DateTime.Now.Date)

But the issue is, is there a method to apply a pre-defined filtering/condition to all queries that go through?ctx.News ? Describe my desire for any inquiries toctx.News to apply the implicit "Published Today" filtering?

12/1/2014 2:38:30 PM

Popular Answer

There is no method to automatically add a condition (filter) to a news query. All samples presented function, but only when you query.News directly. For instance, if you load a navigation property that points toNews Examples won't work.

EDMX uses the formula Constrained mapping to resolve this, however this has other extremely serious drawbacks. It is similar to TPH being downgraded to a single entity type in that conditional mapping is fixed (cannot be modified without rebuilding the model), and you can only have one condition for each type. Furthermore, it's doubtful that the requirements described in conditional mapping can be applied to "Today". Code-first approaches do not provide conditional mapping.

5/17/2011 8:49:37 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow