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 4028380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:12:47+00:00 2026-05-20T11:12:47+00:00

I have a generic repository an I am trying to add a GetById method

  • 0

I have a generic repository an I am trying to add a GetById method as shown here
C# LINQ to SQL: Refactoring this Generic GetByID method

The problem is my repository does not use System.Data.Linq.DataContext
instead I use System.Data.Entity.DbContext

So I get errors where I try to use

Mapping.GetMetaType

and

return _set.Where( whereExpression).Single();

How can I implement a generic GetById method in CTP5? Should I be using System.Data.Entity.DbContext in my Repository.

Here is the start of my repository class

  public class BaseRepository<T> where T : class
    {

        private DbContext _context;
        private readonly DbSet<T> _set;

        public BaseRepository()
        {
            _context = new MyDBContext();
            _set = _context.Set<T>();

        }
  • 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-20T11:12:48+00:00Added an answer on May 20, 2026 at 11:12 am

    The most basic approach is simply

    public T GetById(params object[] keys)
    {
      _set.Find(keys);
    }
    

    If you know that all your entities have primary key called Id (it doesn’t have to be called Id in DB but it must be mapped to property Id) of defined type you can use simply this:

    public interface IEntity
    {
      int Id { get; }
    }
    
    public class BaseRepository<T> where T : class, IEntity
    {
      ...
    
      public T GetById(int id)
      {
        _set.Find(id);
      }
    }
    

    If data type is not always the same you can use:

    public interface IEntity<TKey>
    {
      TKey Id { get; }
    }
    
    public class BaseRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
    {
      ...
    
      public TEntity GetById(TKey id)
      {
        _set.Find(id);
      }
    }
    

    You can also simply use:

    public class BaseRepository<TEntity, TKey> where TEntity : class
    {
      ...
    
      public TEntity GetById(TKey id)
      {
        _set.Find(id);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen generic repository pattern here . I am trying to add generic
I am trying to Implement a Generic Repository with Linq To SQL. My problem
I have this method to get a generic repository out of a dictionary: public
I'm trying to create an update method in a generic repository as a LINQ
I have a generic repository and would like to implement a generic GetById method.
I'm trying to write a generic repository for my Entity Framework based application. Here's
I have a Generic repository and are trying to cast the .Returns to a
I'm trying to implement a Generic Repository. This is what I've got so far
Hello im trying to build a generic repository on top of linq2sql. I have
I have the following problem: I have a generic EF repository using DbContext designed

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.