在使用EF代码时,我会在不同时间收到以下错误:
实体类型SomeType不是当前上下文的模型的一部分.
导致此错误的可能原因是什么?
This is a MODEL first approach. I have already researched this extensiely and have not come up with an answer. I have tried all the suggestions at the following links:
This appears to be the same problem but with no resolution The entity type <classname> is not part of the model for the current context
These are the links I have already researched so please don't answer as duplicate of ---- EF 4.1 Code First error - The entity type …
实体类型<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)