title: EntitySet System.InvalidOperationException - "the entity type is not part of the model for the current context"
body: <h3>Similar Questions</h3>
  
  <p><a href="https://stackoverflow.com/questions/4983108/the-entity-type-classname-is-not-part-of-the-model-for-the-current-context">The entity type &lt;classname&gt; is not part of the model for the current context</a> -and- <a href="https://stackoverflow.com/questions/5634392/ef-4-1-code-first-error-the-entity-type-sometype-is-not-part-of-the-model-for">EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context</a> are similar questions but they are "code first" perspective only, with much simpler data models, and address connection string and mapping issues. Please look closely at this one.</p>
  
  <h3>Symptom</h3>
  
  <pre><code>// HomeController.cs
  public ActionResult Index()
  {
      var _db = new MealsContext();
  
      var m = _db.Meals.ToList();
      var d = _db.Drinks.ToList();
  
      return View();
  }
  </code></pre>
  
  <p>Exception is thrown retrieving the <code>Drinks</code> collection:</p>
  
  <pre><code>The entity type Drink is not part of the model for the current context.
  </code></pre>
  
  <h3>Code</h3>
  
  <pre><code>// Meal.cs
  public class Meal
  {
      public int Id { get; set; }
      public string Stuff { get; set; }
      public virtual ICollection&lt;Meat&gt; Meats { get; set; }
      public virtual ICollection&lt;Vegetable&gt; Vegetables { get; set; }
  }
  
  // Meat.cs
  public class Meat
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int MealId { get; set; }
  }
  
  // Vegetable.cs
  public class Vegetable 
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int MealId { get; set; }
  }
  
  // Drink.cs
  public class Drink
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><em>Yes, I know in the real world the relationship between Meat and Vegetable with Meals would likely be Many-to-Many but don't get hung up on it here.</em></p>
  
  <pre><code>// MealsContext.cs
  public class MealsContext: DbContext
  {               
      public MealsContext() : base("ConnectionString")
  
      public DbSet&lt;Meal&gt; Meals{ get; set; }
      public DbSet&lt;Meat&gt; Meats{ get; set; }
      public DbSet&lt;Vegetable&gt; Vegetables { get; set; }
      public DbSet&lt;Drink&gt; Drinks{ get; set; }
  }
  </code></pre>
  
  <p>My experience was in using a Model First methodology. The EDMX file was built then the POCOs. </p>
  
  <p>In the connection string is the metadata section that maps to the parsed EDMX resources (<code>metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl;</code>).</p>
  
  <p>I examined the underlying XML of the EDMX file shows all entities present in Conceptual and Store models, and all are fully mapped. WTF?</p>
  
  <h3>Troubleshooting</h3>
  
  <p>The first tried was to completely get rid of the store and mapping EDMX data (the <code>SSDL</code> and <code>MSL</code> sections). Fire away, and now there are two exceptions:</p>
  
  <ol>
  <li><p>Retrieving <code>Meals</code> throws <code>MSL, error 2062 No mapping specified for instance of the EntitySet and AssociationSet in the EntityContainer</code>.</p></li>
  <li><p>Retrieving <code>Drinks</code> continues to throw <code>The entity type Drinkis not part of the model for the current context</code>.</p></li>
  </ol>
  
  <p>The error thrown by <code>Meals</code> is expected, I nuked the mappings and store model -- examining <code>_db</code> shows me that <code>Meals</code> -> <code>InternalSet</code> -> <code>EntitySet</code> property is correct, just not mapped.</p>
  
  <p>The error thrown by <code>Drinks</code> is where I am stuck. Examining <code>_db</code> closer shows me that <code>Drinks</code> -> <code>InternalSet</code> -> <code>EntitySet</code> throws the <code>SystemInvalidOperation</code> exception that states the entity is not in the model context.</p>
  
  <p>Here's what the EDMX's CSDL looks like in XML format:</p>
  
  <pre><code>&lt;edmx:ConceptualModels&gt;
    &lt;Schema ...&gt;
      &lt;EntityContainer Name="MealsContext" annotation:LazyLoadingEnabled="true"&gt;
        &lt;EntitySet Name="Meals" EntityType="Models.Meal" /&gt;
        &lt;EntitySet Name="Meats" EntityType="Models.Meat" /&gt;
        &lt;EntitySet Name="Vegetables" EntityType="Models.Vegetable" /&gt;
        &lt;EntitySet Name="Drinks" EntityType="Models.Drink" /&gt;
        &lt;!-- AssociationSets here for the FKs --&gt;
      &lt;/EntityContainer&gt;
      &lt;!-- All are present, but here's the culprit Drink --&gt;
      &lt;EntityType Name="Drink"&gt;
        &lt;Key&gt;
          &lt;PropertyRef Name="Id" /&gt;
        &lt;/Key&gt;
        &lt;Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /&gt;
        &lt;Property Type="String" Name="Name" Nullable="false" MaxLength="200" FixedLength="false" Unicode="true" /&gt;
      &lt;/EntityType&gt;
      &lt;!-- Associations here --&gt;
    &lt;/Schema&gt;
  &lt;/edmx:ConceptualModels&gt;
  </code></pre>
  
  <h2>Question</h2>
  
  <p>If the <code>DbContext</code> has all the <code>DbSet</code> properties and is consuming a connection string that includes metadata for a model who's CSDL correctly defines the entity type <code>Drink</code>, <strong><em>why in the hell is it not part of the context?</em></strong></p>
  
  <p>The only thing different about <code>Drink</code> that I can see is that it is not related to any other entities, and has no associations...</p>