title: Getting DbContext Set<T> by Interface
body: <p><strong>Background</strong></p>
  
  <p>I have a multi-layered application that I am trying to refactor. My Data layer is essentially just a EF Database Context (and all of its entities).
  One of the goals I am trying to accomplish is to remove the dependency between my Biz layer and my Data layer (using Dependency Injection) so that my unit tests do not have to hit the database. To do this, I created a simple interface that my context implements, IDataStore. </p>
  
  <pre><code>public interface IDataStore
  {
     void Insert&lt;T&gt;(T entity) where T:class;
     void Update&lt;T&gt;(T entity) where T:class;
     void Delete&lt;T&gt;(T entity) where T:class;
     T GetById&lt;T&gt;(int id) where T:class;
     IQueryable&lt;T&gt; All&lt;T&gt;() where T:class;
  }
  
  public partial class MyContext:DbContext,IDataStore
  {
     public void Insert&lt;T&gt;(T entity) where T:class{
        this.Set&lt;T&gt;().Add(entity);
        this.SaveChanges();
     }
     ...
     public IQueryable&lt;T&gt; All&lt;T&gt;() where T:class{
        return this.Set&lt;T&gt;();
     }
  }
  </code></pre>
  
  <p>Using an IoC contianer, I was able to remove all refrences in my Biz layer to MyContext (Instead refering to IDataStore), but I'm still stuck refering to my specific entity classes. In the example below, I'm using an IDataStore, but I'm also using a Data.Color (one of my entity classes).</p>
  
  <pre><code>public static void MyBizMethod(){
     ...
     IDataStore store = myUnityContainer.Resolve&lt;IDataStore&gt;();
     List&lt;string&gt; colorNames = store.All&lt;Data.Color&gt;().Select(c =&gt; c.name).ToList();
     ...
  }
  </code></pre>
  
  <p>The next thing I did was create an IColor interface and used a partial class to make Data.Color implement IColor. I then modified my Biz-layer logic to call DataStore.All&lt; IColor>() instead of DataStore.All&lt; Data.Color>();</p>
  
  <pre><code>public interface IColor{
     string name;
  }
  
  public partial class Color: IColor { }
  </code></pre>
  
  <p>and in my Biz layer....</p>
  
  <pre><code>public static void MyBizMethod(){ 
     ...
     List&lt;string&gt; colorNames = store.All&lt;IColor&gt;().Select(c =&gt; c.name).ToList();
     ...
  }
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>When DbContext.Set&lt; T>() gets called using an interface for T (In my case IColor) i get an exception because the context has a DbSet&lt; Color> not DbSet&lt; IColor>. </p>
  
  <p>Is there someway make the context have a DbSet&lt; IColor> instead? If not, is there some other way to genericly retrieve an DbSet from a DbContext by interface? I'd really like to avoid writing a giant switch statement.</p>
  
  <p>Or... Is there a better way to do this altogether? Preferably one that doesn't involve creating a seperate interface for every type of entity I have,</p>
  
  <p><strong>Update</strong></p>
  
  <p>The post left by @doctor addresses my initial question (how to query a table by interface), but the other posts explain that that is not even necessary (since my entity classes are already abstracted anyway) and I feel like they helped more with the root of my problem</p>
  
  <p>Nevertheless, I wanted to see if the other post's solution would work for what I had originally asked, and it does... for the most part. The notable exception is the .Include() method for IQueryable.</p>
  
  <p>Consider the following code example:</p>
  
  <pre><code>var people = store.All&lt;IPerson&gt;().Where(p =&gt; p.IsAlive).Include("Children").ToList();
  foreach (IPerson parent in people)
  {
     Console.WriteLine(parent.Name + " has " + p.Children.Count() + " children");
  }
  </code></pre>
  
  <p>When calling store.All&lt; Person>(), the .Include() gets translated to SQL as would be expected. When using IPerson however, it appears to be completely ignored and a separate query is generated every time through the loop. The funny thing is that the .Where() clause gets correctly translated in both cases (I verified using SQL profiler). Anyway, I've decided not to implement a separate interface for every entity I have and instead reference the entities directly. Just wanted to point out the odd .Include() behavior. </p>