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?
Related videos on Youtube
Author by
Fox
Updated on June 04, 2022Comments
-
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 over 12 yearsThis 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 over 12 years@Fox Then you can manually change the name or give a different name for
IQueryable<Assignee>
property. -
linkerro over 12 yearsWhy don't you create a class that inherits from the generated code and override the behaviour of the property?
-
Fox over 12 yearsToo much code changes.. i have to change a lot of code in my DAL... good idea though
-
linkerro over 12 yearsI 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 over 12 yearsThere 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 over 9 yearsThe 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 over 7 yearsNice one, it works perfectly. I just set the DbSet protected to expose only one property from DbContext for intellisense pourpose ;)
-
Aaron Hudon almost 6 years.Where returns IQueryable, the return type for Assignees is DbSet