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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:02:01+00:00 2026-05-28T07:02:01+00:00

I am writing a generic repository for entity framework and am confused as to

  • 0

I am writing a generic repository for entity framework and am confused as to what the difference between these calls are:

ObjectContext.CreateObjectSet<T>
ObjectContext.CreateQuery<T>
DbContext.Set<T>

I want a generic repository that both supports context generated from .edmx files as well as code first DbContext, so I’ve got this:

 public abstract class EntityRepository<TClass>
   where TClass : class, new()
{
    //private readonly TContext _context;
    private readonly ObjectSet<TClass> _objectSet;

    protected EntityRepository(IObjectContextAdapter context)
    {
        _objectSet = context.ObjectContext.CreateObjectSet<TClass>();

    }

    protected EntityRepository(ObjectContext context)
    {
        _objectSet = context.CreateObjectSet<TClass>();
    }

    public ObjectSet<TClass> Query()
    {
        return _objectSet;
    }
}

In the examples I have seen online I’ve seen all 3 used, what is the actual differences between them? Is one better performance wise? I know you can write LINQ queries against the contexts using all 3 methods.

  • 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-28T07:02:02+00:00Added an answer on May 28, 2026 at 7:02 am

    The CreateObjectSet<T> returns you ObjectSet<T> that’s basically collection of T objects, with ability to add, remove, … object from this collections resulting later to inserts, deletes, … You can also use it for querying. It’s like a top level root for given entity.

    The CreateQuery<T> gives you ObjectQuery<T>, which can be viewed like IEnumerable<T> (it’s also IQueryable<T>). This object is like subset of ObjectSet<T> (some conditions etc.), but you can’t add items to it and so on.

    And finally the Set<T> returns DbSet<T> that’s simplified version of first method/object for Code First. It’s easier to, for instance, to use these objects (or better to say interfaces; IDbSet<T>) for i.e. unit testing etc. Similar to what ObjectContext and DbContext is.

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

Sidebar

Related Questions

I am learning repository pattern and was reading Repository Pattern with Entity Framework 4.1
I'm currently writing a generic vector template class (the geometric entity, not the container)
I am writing a generic repository to be used for my every model CRUD
I decided to start writing unit tests in our application. It uses Entity Framework
I was writing a generic class to read RSS feed from various source and
I'm writing a generic class where I need to use Interlocked. T test1, test2;
I am writing a generic Http resource hosting service and am storing larger objects
I'm writing a generic matrix class in c++. I want to be able to
I'm writing an generic Array class and overloading operators for convenience. I've gotten my
My question is eerily similar to Writing a generic class to handle built-in types

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.