Entity Framework 4.1 - Override Entity (DBSet) with Filter

11,692

Solution 1

Try exposing DbSet<Assignee> and IQueryable<Assignee> with different names

public partial class MyEntities: DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    {
    }

    public DbSet<Assignee> AssigneesSet { get; set; }

    public IQueryable<Assignee> Assignees 
    {
        get
        {
            return AssigneesSet.Where(z => z.IsActive == true);
        }
    }
}

Solution 2

Have you tried adding a Condition to the Table Mapping in your model? Right click the entity in your edmx and choose "Table Mapping". Then "Add a condition". Probably a more elegant solution.

Solution 3

public override DbSet<Assignee> Assignees 
{
    get
    {
        return base.Assignees.Where(z => z.IsActive == true);
    }
    set; 
}

This what you want?

Share:
11,692

Related videos on Youtube

Fox
Author by

Fox

Updated on June 04, 2022

Comments

  • Fox
    Fox almost 2 years

    I'm trying to do something which should be relatively easy, but i just dont know how to construct it.

    I have a Generated Entity which I'd like to override by adding a Linq Where statement.

    Herewith the partial for the Context :

    public partial class MyEntities: DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }    
        public DbSet<Assignee> Assignees { get; set; }
    }
    

    I've created a new partial of MyEntities and tried the following

    public override DbSet<Assignee> Assignees 
    {
        get
        {
            return this.Assignees.Where(z => z.IsActive == true);
        }
        set; 
    }
    

    but this throws an ambiguity error (which is obvious).

    How can I accomplish this?

    Thanks

  • Fox
    Fox over 12 years
    This seems the most logical... Not sure if this is going to work though because EF will still generate public DbSet<Assignee> Assignees { get; set; } and wont that be used when calling context.Assignees ?
  • Eranga
    Eranga over 12 years
    @Fox Then you can manually change the name or give a different name for IQueryable<Assignee> property.
  • linkerro
    linkerro over 12 years
    Why don't you create a class that inherits from the generated code and override the behaviour of the property?
  • Fox
    Fox over 12 years
    Too much code changes.. i have to change a lot of code in my DAL... good idea though
  • linkerro
    linkerro over 12 years
    I don't think you're going to be able to do it any other way, since you would be messing with the hooks EF needs on the classes.
  • linkerro
    linkerro over 12 years
    There might be another way to do this, and that is to change the code that EF emits. Here's an article that explains it: blogs.microsoft.co.il/blogs/gilf/archive/2009/12/05/…
  • demius
    demius over 9 years
    The problem with this approach is that EF doesn't allow you to add filters for mapped properties. In this example, he would have to remove the IsActive column from the mapping, which means that EF won't save changes to the IsActive field. I've been banging my head against my desk for hours trying to wangle a way around it, but to no avail.
  • ing.alfano
    ing.alfano over 7 years
    Nice one, it works perfectly. I just set the DbSet protected to expose only one property from DbContext for intellisense pourpose ;)
  • Aaron Hudon
    Aaron Hudon almost 6 years
    .Where returns IQueryable, the return type for Assignees is DbSet