none
WebAPI, EF 5 Code First & Conditional Navigation Properties RRS feed

  • Question

  • I am writing a webservice, which uses EF code first to pull data from a database, this part of the process I have working fine. My issue comes is with navigation properties and tweaking the way they work slightly.

    Each item in the database has an IsDeleted entry as a boolean, because I don't want to actually delete the entries when the end user does.

    The problem I then have is that I don't want to return those items when a user calls the webservice, and this needs to apply to collections on parent objects, so if for example I have the following objects;

    public class Film
    {
       public int FilmID { get; set; }
       public virtual ICollection<Actors> Actors { get; set; }
    }
    
    public class Actor
    {
       public int ActorID { get; set; }
       public string Name { get; set; }
       public bool IsDeleted { get; set; }
    }

    I only want the EF to return the actors in the collection where IsDeleted == false;

    Is there a way to tweak it to do this ?

    Many thanks in advance,

    Neil.

    Friday, September 20, 2013 8:33 AM

Answers

  • If you have turned lazy loading off you can set a filter on the query when loading the navigation collection. See this article for more.

        context.Entry(film)
            .Collection(f => f.Actors)
            .Query()
            .Where(a => a.IsDeleted == false)
            .Load();

    Friday, September 20, 2013 9:35 AM

All replies

  • If you have turned lazy loading off you can set a filter on the query when loading the navigation collection. See this article for more.

        context.Entry(film)
            .Collection(f => f.Actors)
            .Query()
            .Where(a => a.IsDeleted == false)
            .Load();

    Friday, September 20, 2013 9:35 AM
  • Many thanks for your reply, this is just what I was looking for.

    Just one further question, I am using the EF within a repository pattern implementation, where I inherit my repository from a RepositoryBase<T>.  To implement this should I override the routines within my repository and put this code in there, or should I be doing something outside of the repository, in which case wouldn't I need to somehow expose the Entry method ?

    Apologies for what might seem a really silly question but I'm very new to this pattern and am learning as I go along.

    Many thanks,

    Neil.

    Friday, September 20, 2013 12:05 PM
  • I guess that really depends on the usage of the repository. If you want to make sure anyone using the repository does not load collections including those marked for deletion then just load the navigation collections in the repository when you return the entity. If you want them to have more control you could return the entities with the navigation collections unloaded and then provide a method on the repository to allow users to load up the collections themselves.

    Friday, September 20, 2013 11:55 PM