实体类型<classname>不是当前上下文的模型的一部分 - 并且 - EF 4.1 Code First错误 - 实体类型SomeType不是当前上下文的模型的一部分是类似的问题但它们是"代码优先"透视只有更简单的数据模型,并解决连接字符串和映射问题.请仔细看看这个.
// HomeController.cs
public ActionResult Index()
{
var _db = new MealsContext();
var m = _db.Meals.ToList();
var d = _db.Drinks.ToList();
return View();
}
Run Code Online (Sandbox Code Playgroud)
抛出异常会检索Drinks集合:
The entity type Drink is not part of the model for the current context.
Run Code Online (Sandbox Code Playgroud)
// Meal.cs
public class Meal
{
public int Id { get; set; }
public string Stuff { get; set; }
public virtual ICollection<Meat> Meats { get; set; …Run Code Online (Sandbox Code Playgroud)