title: Generic method to retrieve DbSet<T> from DbContext
body: <p>I'm using the Entity Framework with a large database (made up of more than 200 tables).</p>
  
  <p>Trying to create a generic method that returns the <code>DbSet&lt;T&gt;</code> of a specific table <code>T</code> (i.e. class, which can be <code>TableA</code>).</p>
  
  <p>The entity class that was (automatically) created using the entity data model looks like so:</p>
  
  <pre><code>public partial class sqlEntities : DbContext
  {
  
      public virtual DbSet&lt;TableA&gt; TableA { get; set; }
      public virtual DbSet&lt;TableB&gt; TableB { get; set; }
      public virtual DbSet&lt;TableC&gt; TableC { get; set; }
      ... // other methods
  
  }
  </code></pre>
  
  <p>My main class is like this</p>
  
  <pre><code>public class TableModifier
  {
     // Should return first 10 elements from a table of that type T
     public IQueryable&lt;T&gt; GetFromDatabase&lt;T&gt;() where T : EntityObject
     {
         try
         {
             using (sqlEntities ctx = new sqlEntities())
             {
                 // Get the DbSet of the type T from the entities model (i.e. DB)
                 DbSet&lt;T&gt; dbSet = ctx.Set&lt;T&gt;();
                 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
  }
  </code></pre>
  
  <p>I have to set a constraint <code>where T : EntityObject</code> on <code>T</code> to be within the <code>EntityObject</code> bounds. If there was no such constraint then the <code>DbSet&lt;T&gt; dbSet</code> would complain (i.e. <em>T must be a reference type</em>) that it might be getting more than it expects in terms of types (<a href="http://blog.damianbrady.com.au/2011/08/16/using-generics-for-lookup-tables-in-entity-framework/comment-page-1/" rel="noreferrer">based</a> on this).</p>
  
  <p>The problem occurs when I try to actually call the method with a specific type.</p>
  
  <pre><code> [TestMethod]
   public void Test_GetTest()
   {
       TableModifier t_modifier = new TableModifier();
  
       // The get method now only accepts types of type EntityObject
       IQueryable&lt;TableA&gt; i_q = t_modifier.GetFromDatabase&lt;TableA&gt;();
   }
  </code></pre>
  
  <p>It gives an error:</p>
  
  <pre><code>There is no implicit reference conversion from 'TableMod.TableA' to
  'System.Data.Entity.Core.Objects.DataClasses.EntityObject'.
  </code></pre>
  
  <p>How can I (cast?) the <code>TableA</code> type as an <code>EntityObject</code> if I know it exists for that entity model?</p>
  
  <p>Though this is incorrect syntax (and logic) this is what I'm after:<br></p>
  
  <pre><code> t_modifier.GetFromDatabase&lt;(EntityObject)TableA&gt;();
  </code></pre>
  
  <p>How can I define the <code>TableA</code> (along with all the other 200 tables) type to be a part of <code>EntityObject</code>?</p>
  
  <hr>
  
  <p><strong>A potential solution</strong></p>
  
  <p>Turns out my constraint was too specific, all I needed to change was from <code>where T : IEntity</code> to</p>
  
  <pre><code>where T : class
  </code></pre>
  
  <p>So the <code>T</code> is what the <code>DbSet&lt;T&gt;</code> initially expected, a class type</p>
  
  <p>Saves the trouble of having to add implementations to the 200+ table classes, <code>TableA</code>, <code>TableB</code>, ...</p>
  
  <p>Then of course there's other problems such as changing the return type from <code>IQueryable&lt;T&gt;</code> to <code>List&lt;T&gt;</code> since the <code>IQueryable</code> would otherwise be returned outside of the scope of <code>DbContext</code> (i.e. <code>sqlEntities</code>) rendering it useless.</p>