Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.1 - Override Entity (DBSet) with Filter

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

like image 914
Fox Avatar asked Jan 17 '12 07:01

Fox


2 Answers

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);
        }
    }
}
like image 161
Eranga Avatar answered Oct 12 '22 05:10

Eranga


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.

like image 44
ryanbakernz Avatar answered Oct 12 '22 04:10

ryanbakernz