实体类型List`1不是当前上下文模型的一部分 [英] The entity type List`1 is not part of the model for the current context
问题描述
我一直在使用数据库首先,EF 4.1
I've been using Database First, EF 4.1
我得到的实体类型List`1是不是该机型为当前上下文的一部分。尝试更新从我的编辑,查看记录时出错。
I am getting "The entity type List`1 is not part of the model for the current context." error when trying to update a record from my Edit View.
该错误是在
db.Entry(properties).State = EntityState.Modified;
下面是我的模型:
public class Users
{
[Key]
public int User_ID { get; set; }
public string UserName { get; set; }
[NotMapped]
public IEnumerable<App_Properties> User_Properties
{
get { return Properties.Where(u => u.User_ID == User_ID); }
}
public virtual ICollection<App_Properties> Properties { get; set; }
}
public class App_Properties
{
[Key]
public int Prop_ID { get; set; }
public int User_ID { get; set; }
public int App_ID { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public DateTime DateEntered { get; set; }
public DateTime DateModified { get; set; }
[ForeignKey("User_ID")]
public virtual Users Users { get; set; }
}
下面是我的控制器:
[HttpPost]
public ActionResult Edit(ICollection<App_Properties> properties)
{
if (ModelState.IsValid)
{
foreach (var item in properties)
{
db.Entry(properties).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(properties);
}
我怀疑foreach循环是不是在设置为EntityState在ICollection的每个项目为宜。
I suspect the foreach loop is not appropriate in setting the EntityState for each item in an ICollection.
任何援助将大大AP preciated。
Any assistance would be greatly appreciated.
推荐答案
试着改变你的循环:
foreach (var item in properties)
{
db.Entry(item).State = EntityState.Modified;
}
您分别致电 db.Entry(属性)
,所以你试图同时附上整个集合。 DbContext.Entry(对象)需要一个单一的对象,不集合。
You were calling db.Entry(properties)
, so you were trying to attach the whole collection at once. DbContext.Entry(object) expects a single object, not a collection.
这篇关于实体类型List`1不是当前上下文模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!