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
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);
}
}
}
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.
public override DbSet<Assignee> Assignees
{
get
{
return base.Assignees.Where(z => z.IsActive == true);
}
set;
}
This what you want?
A good practice is to create repository classes in a DAL folder(use the name you want). Then do the filters there. Here is a microsoft tutorial for that:
Depending of the condition you want is not possible to put on EF mapping condition, here is the reason: http://entityframework.codeplex.com/workitem/48
I know this is super old, but another simple and elegant way, without changing any existing names, is using the new
keyword to hide the original member, like this:
public new IQueryable<Assignee> Assignees
{
get
{
return base.Assignees.Where(z => z.IsActive == true);
}
}
Just wanted to share for any future visitors, hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.