What are the distinctions between.CreateObjectSetT>,.SetT>, and.CreateQueryT>?

datacontext entity-framework generics


I'm creating a generic repository for entity framework and don't understand what makes these calls different from one another:


I've got this because I want a generic repository that can handle both code first DbContext and context created from.edmx files:

 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;

All three have been utilized in the instances I've seen online; what are their true distinctions? Is one performing better? I am aware that all three ways may be used to create LINQ queries against the contexts.

1/13/2014 6:29:52 PM

Accepted Answer

The CreateObjectSet<T> back to youObjectSet<T> Essentially, it is a collection of T objects with the option to add, delete, or remove things from the collection. It may also be used for searching. It functions as the entity's top-level root.

The CreateQuery<T> offers youObjectQuery<T> It might be interpreted asIEnumerable<T> (It also isIQueryable<T> This item resembles a subset ofObjectSet<T> (certain restrictions, etc.), but you cannot add things to it, etc.

And lastly, theSet<T> returns DbSet<T> That is a condensed version of Code First's first method or object. For example, using these objects, or better yet, interfaces, is simpler.IDbSet<T> , such as unit testing, etc. comparable to whatObjectContext and DbContext is.

1/21/2012 6:13:23 AM

Popular Answer

In addition to what Jiri previously said, CreateObjectSet has an overload that accepts no parameters. The default assumption for this overload is that it must return the ObjectSet for the single EntitySet linked to TEntity. Whenever the model possesses MEST, it will toss (multiple-entity-sets-per-type). To bootstrap the query, each overload of CreateQuery requires an Entity SQL string (note that the name of an EntitySet is a valid Entity SQL query).

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow