9

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

Eranga
  • 32,181
  • 5
  • 97
  • 96
Fox
  • 891
  • 3
  • 9
  • 30

5 Answers5

11

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);
        }
    }
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • This seems the most logical... Not sure if this is going to work though because EF will still generate public DbSet Assignees { get; set; } and wont that be used when calling context.Assignees ? – Fox Jan 17 '12 at 07:42
  • @Fox Then you can manually change the name or give a different name for `IQueryable` property. – Eranga Jan 17 '12 at 07:50
  • Nice one, it works perfectly. I just set the DbSet protected to expose only one property from DbContext for intellisense pourpose ;) – ing.alfano Oct 28 '16 at 10:29
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.

  • 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. – demius Aug 22 '14 at 16:47
1
public override DbSet<Assignee> Assignees 
{
    get
    {
        return base.Assignees.Where(z => z.IsActive == true);
    }
    set; 
}

This what you want?

linkerro
  • 5,318
  • 3
  • 25
  • 29
  • I can't do that because I'm not inheriting from MyEntities... Inheriting from DbContext – Fox Jan 17 '12 at 07:39
  • 1
    Why don't you create a class that inherits from the generated code and override the behaviour of the property? – linkerro Jan 17 '12 at 08:07
  • Too much code changes.. i have to change a lot of code in my DAL... good idea though – Fox Jan 17 '12 at 08:11
  • 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 Jan 17 '12 at 08:13
  • 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: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/12/05/t4-templates-in-entity-framework-4.aspx – linkerro Jan 17 '12 at 08:16
  • .Where returns IQueryable, the return type for Assignees is DbSet – Aaron Hudon May 30 '18 at 05:33
0

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:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

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

Vinicius Sin
  • 1,571
  • 11
  • 8
0

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!

Nahuel Prieto
  • 371
  • 3
  • 15