Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:07:32+00:00 2026-05-23T01:07:32+00:00

I am trying to figure out how to use the new EF Code First

  • 0

I am trying to figure out how to use the new EF Code First stuff and I am having trouble figuring out how to adapt the Include functionality into a semi-generic Repository class. (I say semi-generic, because the class is not generic, just the methods. So I have one repository that wraps an aggregate, essentially, and you can interact with all Entities that are part of that aggregate, not just a single Entity.)

My situation is I have an Entity that has one child that always needs to be loaded, and then other children that are only optionally loaded, I want to include a simple bool parameter on my repository method. Like so:

public S GetByID<S>(int entityID, bool loadChildren = false) where S : class
{
  DbSet<S> set = _context.Set<S>();

  if (loadChildren)
    set = FullInclude<S>(set);
  else
    set = DefaultInclude<S>(set);

  return set.Find(entityID);
}

protected virtual DbSet<S> DefaultInclude<S>(DbSet<S> set) where S : class
{
  // base implementation just returns the set
  // derived versions would attach Includes to the set before returning it
  return set;
}

protected virtual DbSet<S> FullInclude<S>(DbSet<S> set) where S : class
{
  // base implementation just returns the set
  // derived versions would attach Includes to the set before returning it
  return set;
}

That is my base Repository, and I’d like to be able to override those XyzInclude methods in a derived class. But I need to override them with a non-generic implementation, and this obviously doesn’t work at a language level.

This was really easy with Linq2Sql in that I’d just configure a DataLoadOptions object and attach it to the context. With the Include API off of DbSet I’m stumped on how best to do this. I’m looking for recommendations on a simple implementation of a Strategy pattern or similar.

EDIT: I guess the essence of my question is really about overriding a generic method with a non-generic derived version. I’m looking for a pattern or technique that allows me to do that. Extension methods are one thing I’m looking at, but would prefer a more “pure” solution if anyone has one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:07:32+00:00Added an answer on May 23, 2026 at 1:07 am

    Just thought I’d revisit this with the solution I ended up with. My repositories aren’t fully generic, but all the methods are, so I needed a way to store includes for any entity type and be able to pull them out when the methods were called for that type.

    I borrowed from Ladislav’s answer here, and I may revisit this design to just have each individual method defined on the derived repositories define their own includes as there are enough different combinations of what to include and what not to include that repeating the little bit of code defining the same include in a few places is probably worth it. But anyway, this is the current design and it works…

    Base repository:

    public abstract class Repository : IQueryableRepository, IWritableRepository
    {
      private readonly DbContext _context;
      private readonly Dictionary<Type, LambdaExpression[]> _includes = new Dictionary<Type, LambdaExpression[]>();
    
      protected Repository(DbContextBase context)
      {
        _context = context;
        RegisterIncludes(_includes);
      }
    
      protected abstract void RegisterIncludes(Dictionary<Type, LambdaExpression[]> includes);
    
      protected S GetSingle<S>(Expression<Func<S, bool>> query, bool getChildren = false) where S : class
      {
        IQueryable<S> entities = _context.Set<S>().AsNoTracking();
    
        if (query != null)
          entities = entities.Where(query);
    
        entities = ApplyIncludesToQuery<S>(entities, getChildren);
    
        return entities.FirstOrDefault();
      }
    
      private IQueryable<S> ApplyIncludesToQuery<S>(IQueryable<S> entities, bool getChildren) where S : class
      {
        Expression<Func<S, object>>[] includes = null;
    
        if (getChildren && _includes.ContainsKey(typeof(S)))
          includes = (Expression<Func<S, object>>[])_includes[typeof(S)];
    
        if (includes != null)
          entities = includes.Aggregate(entities, (current, include) => current.Include(include));
    
        return entities;
      }
    }
    

    Derived repositories only need to define their includes in the one place, and then when you call the query method you just specify if you want children included or not (see getChildren above).

    public class DerivedRepository : Repository
    {
      public DerivedRepository(DbContext context)
        : base(context) { }
    
      protected override void RegisterIncludes(Dictionary<Type, LambdaExpression[]> includes)
      {
        includes.Add(typeof(ParentType), new Expression<Func<ParentType, object>>[] { 
          p => p.SomeChildReference.SomeGrandchild,
          p => p.SomeOtherChildReference
        });
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Rails development, and I'm trying to figure out how to use
I've been having a lot of problems trying to figure out how to use
I am trying to figure out something with EF4 Code Only. If i use
I'm trying to figure out how to decide when to use NSDictionary or NSCoder/NSCoding?
I am still really new to SQL functions. I am trying to figure out
I'm trying to figure out the correct syntax to use the pipe operator |>
Trying to figure out an equation to get the current group a page would
In trying to figure out this problem (which is still unsolved and I still
I'm trying to figure out how big a certain database would be (it hasn't
I'm basically trying to figure out the simplest way to perform your basic insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.