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

  • SEARCH
  • Home
  • 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 8388837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:33:30+00:00 2026-06-09T18:33:30+00:00

I have two Entity Framework 5 Get() methods that perform (i) a single entity

  • 0

I have two Entity Framework 5 Get() methods that perform (i) a single entity get by ID, and (ii) a single entity get via a filter with any eager loading bolted on. See below for the code:

internal readonly FallenNovaContext Context;
private readonly DbSet<TEntity> _dbSet;

internal GenericRepository(FallenNovaContext context)
{
    Context = context;
    _dbSet = context.Set<TEntity>();
}

// (i) Get by ID.
public TEntity GetById(int id)
{
    return _dbSet.Find(id);
}

// (ii) Get by filter and optional eager loading includes.
public TEntity Get(
    Expression<Func<TEntity, bool>> filter = null,
    IEnumerable<string> includePaths = null)
{
    IQueryable<TEntity> query = _dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    if (includePaths != null)
    {
        query = includePaths.Aggregate(query, (current, includePath) => current.Include(includePath));
    }

    return query.SingleOrDefault();
}

All of which works fine now what I’m finding as my application grows is I’m writing a lot of non-generic methods that need a mix of both – more specifically I want a generic get by ID and also be able to eager load related entities.

So the method signature would look something like this:

 public TEntity GetById(
     int id,
     IEnumerable<string> includePaths)
 {
       // ???
 }

Which I could call like this:

 User user = UnitOfWork.UserRepository.GetById(117, new List<string>() { "UserRole", "UserStatus" });

Or like this:

 Car car = UnitOfWork.CarRepository.GetById(51, new List<string>() { "Make", "Model", "Tyres" });

Any help on the suggestions of how I use Entity Framework 5 to code the logic for the TEntity GetById(int id, IEnumerable includePaths) method would be appreciated.

  • 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-06-09T18:33:32+00:00Added an answer on June 9, 2026 at 6:33 pm

    First, write a base class for entities, which defines the primary key field. Something like the following may work:

    public abstract class BaseEntity
    {
        public int Id {get;set;}
    }
    

    Then, write a base class for your repositories; define all generic methods in this base repository. Let this repository have a generic parameter of entity type:

    public class RepositoryBase<TEntity> where TEntity : BaseEntity
    {
       public TEntity GetById(
         int id,
         params Expression<Func<TEntity, object>>[] includeList)
         {
                TEntity entity = null;
                ObjectQuery<TEntity> itemWithIncludes = context.Set<TEntity>() as ObjectQuery<TEntity>;
                foreach (Expression<Func<TEntity, object>> path in includeList)
                {
                    itemWithIncludes = ((IQueryable)itemWithIncludes.Include(path)) as ObjectQuery<T>;
                }
    
                IQueryable<TEntity> items = itemWithIncludes.AsQueryable<TEntity>();
                entity = items.Where(p => p.Id == id).SingleOrDefault();
                return entity;
         }
    }
    

    Update: @Bern asked whether there is any other way to find primary key than declaring a base class. The following questions refer to this problem.

    Entity Framework 4: How to find the primary key?

    Entity Framework code first. Find primary key

    On the otherhand I do not know if there is any other way in EF 5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that are considered a single entity in my domain model.
I am using Entity Framework and I have two tables, a contact relationship table
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I'm using Entity Framework 4 and having trouble with a query. I have two
I am using Entity framework on a forum website and have two query examples
I'm using ASP.NET MVC 3 with Entity Framework CodeFirst I have two classes as
I have two entities referenced one to many. When entity framework created the table
I am new to entity framework. I have two tables... public class S7_Baskets {
I have an Entity Framework v1 project. I have two entities (Roles and Permissions),
I have two classes created by Entity Framework based off tables in my db

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.