Related questions
The entity type <classname> is not part of the model for the current context -and- EF 4.1 Code The first error. The SomeType object type is not part of the model for the current context. are similar questions, but they are only in terms of "code", with much simpler data models, as well as a connection address bar and display. Look carefully at that.
Symptoms
// HomeController.cs public ActionResult Index() { var _db = new MealsContext(); var m = _db.Meals.ToList(); var d = _db.Drinks.ToList(); return View(); }
An exception was thrown from the Drinks collection:
The entity type Drink is not part of the model for the current context.
The code
// Meal.cs public class Meal { public int Id { get; set; } public string Stuff { get; set; } public virtual ICollection<Meat> Meats { get; set; } public virtual ICollection<Vegetable> 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; } }
Yes, I know that in the real world, the relationship between meat and vegetables with dishes is likely to be โMany for many,โ but don't dwell on it here.
// MealsContext.cs public class MealsContext: DbContext { public MealsContext() : base("ConnectionString") public DbSet<Meal> Meals{ get; set; } public DbSet<Meat> Meats{ get; set; } public DbSet<Vegetable> Vegetables { get; set; } public DbSet<Drink> Drinks{ get; set; } }
My experience was using the Model First methodology. The EDMX file was then created by POCOs.
The connection string contains a metadata section that maps to the analyzed EDMX resources ( metadata=res://*/Models.MealsModels.csdl|res://*/Models.MealsModels.ssdl|res://*/Models.MealsModels.msl; ).
I looked at the basic XML of the EDMX file, which shows all the entities present in the Conceptual and Store models, and they are all completely mapped. WTF?
Troubleshooting
First, we tried to completely get rid of the storage and display of EDMX data ( SSDL and MSL sections). Fire, and now there are two exceptions:
Extract Meals throws MSL, error 2062 No mapping specified for instance of the EntitySet and AssociationSet in the EntityContainer .
Extraction Drinks continues to throw The entity type Drinkis not part of the model for the current context .
The error caused by Meals is expected, I displayed the mappings and the storage model - examining _db shows me that the property Meals โ InternalSet โ EntitySet is correct, it just does not display.
The error created by Drinks is where I get stuck. A closer _db shows me that Drinks InternalSet EntitySet throws a SystemInvalidOperation exception, which indicates that the object is not in the context of the model.
Here's what the EDMX CSDL looks like in XML format:
<edmx:ConceptualModels> <Schema ...> <EntityContainer Name="MealsContext" annotation:LazyLoadingEnabled="true"> <EntitySet Name="Meals" EntityType="Models.Meal" /> <EntitySet Name="Meats" EntityType="Models.Meat" /> <EntitySet Name="Vegetables" EntityType="Models.Vegetable" /> <EntitySet Name="Drinks" EntityType="Models.Drink" /> </EntityContainer> <EntityType Name="Drink"> <Key> <PropertyRef Name="Id" /> </Key> <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Type="String" Name="Name" Nullable="false" MaxLength="200" FixedLength="false" Unicode="true" /> </EntityType> </Schema> </edmx:ConceptualModels>
Question
If DbContext has all the DbSet properties and consumes a connection string that includes metadata for the model, which CSDL correctly defines the Drink entity type , why the hell is this not part of the context?
The only difference in Drink that I see is that it is not associated with any other objects and has no associations ...