Generic method to retrieve DbSet from DbContext

c# entity-framework generics


I'm using the Entity Framework with a large database (made up of more than 200 tables).

Trying to create a generic method that returns the DbSet<T> of a specific table T (i.e. class, which can be TableA).

The entity class that was (automatically) created using the entity data model looks like so:

public partial class sqlEntities : DbContext

    public virtual DbSet<TableA> TableA { get; set; }
    public virtual DbSet<TableB> TableB { get; set; }
    public virtual DbSet<TableC> TableC { get; set; }
    ... // other methods


My main class is like this

public class TableModifier
   // Should return first 10 elements from a table of that type T
   public IQueryable<T> GetFromDatabase<T>() where T : EntityObject
           using (sqlEntities ctx = new sqlEntities())
               // Get the DbSet of the type T from the entities model (i.e. DB)
               DbSet<T> dbSet = ctx.Set<T>();
               return dbSet.Take(10);
       catch (Exception ex)
           // Invalid type was provided (i.e. table does not exist in database)
           throw new ArgumentException("Invalid Entity", ex);
   ... // other methods

I have to set a constraint where T : EntityObject on T to be within the EntityObject bounds. If there was no such constraint then the DbSet<T> dbSet would complain (i.e. T must be a reference type) that it might be getting more than it expects in terms of types (based on this).

The problem occurs when I try to actually call the method with a specific type.

 public void Test_GetTest()
     TableModifier t_modifier = new TableModifier();

     // The get method now only accepts types of type EntityObject
     IQueryable<TableA> i_q = t_modifier.GetFromDatabase<TableA>();

It gives an error:

There is no implicit reference conversion from 'TableMod.TableA' to

How can I (cast?) the TableA type as an EntityObject if I know it exists for that entity model?

Though this is incorrect syntax (and logic) this is what I'm after:


How can I define the TableA (along with all the other 200 tables) type to be a part of EntityObject?

A potential solution

Turns out my constraint was too specific, all I needed to change was from where T : IEntity to

where T : class

So the T is what the DbSet<T> initially expected, a class type

Saves the trouble of having to add implementations to the 200+ table classes, TableA, TableB, ...

Then of course there's other problems such as changing the return type from IQueryable<T> to List<T> since the IQueryable would otherwise be returned outside of the scope of DbContext (i.e. sqlEntities) rendering it useless.

7/8/2014 11:29:29 PM

Popular Answer

Why don't you try changing your constrain to class instead of EntityObject

public IQueryable<T> GetFromDatabase<T>() where T : class
3/11/2016 7:11:02 PM

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