entity-frameworkgenericsobjectcontextcode-reuseobjectset

Generic ObjectContext? objectContext.GetObjectSet<TEntity>?


Is there a way to get ObjectQuery<T> for specfied generic type?

Pseudo:

public partial class MyObjectContext
{
    public ObjectSet<TEntity> GetObjectSet<TEntity>()
    {
        return Helper.GetObjectSet<TEntity>(this);
    }
}

Solution

  • Yes this is what you need:

    public partial class MyObjectContext
    {
        public ObjectSet<TEntity> GetObjectSet<TEntity>()
        {
            return this.CreateObjectSet<TEntity>();
        }
    }
    

    As you can see your helper method is not needed because you can call CreateObjectSet directly on MyObjectContext instance. It will return ObjectSet<TEntity> which is derived from ObjectQuery<TEntity>. TEntity must be mapped type and it cannot be derived type in entity hierarchy.