The entity type List`1 is not part of the model for the current context

无人久伴 提交于 2019-11-27 15:41:35

问题


I've been using Database First, EF 4.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.

The error is occurring at

db.Entry(properties).State = EntityState.Modified;

Here is my Model:

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; }
}

Here is my Controller:

[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);
}

I suspect the foreach loop is not appropriate in setting the EntityState for each item in an ICollection.

Any assistance would be greatly appreciated.


回答1:


Try changing your loop to:

foreach (var item in properties)
{
     db.Entry(item).State = EntityState.Modified;
}

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.




回答2:


Thanks, Leniency, for the answer. Worked great.

For what it's worth, I prefer to keep my EntityState.Modified assignments on a single line (as I have multiples) so used the following LINQ:

properties.ForEach(p => db.Entry(p).State = EntityState.Modified);



回答3:


I ended up here despite using code-first EF. The answer to the problem for me was simply not to try to pass a list of entities to the insert method, but insert them one at a time instead:

entityList.ForEach(context.Insert);




回答4:


i think you can use this code for this problem. Because if you use it, you will met another problem. Now i fixed my problem and i hope it will work for you

foreach (var item in properties)
{
  var oldEntity = FGetById(item.Id); // You can use find instead of FGetById
  context.Entry(oldEntity).CurrentValues.SetValues(item);
  Update(oldEntity);
}


来源:https://stackoverflow.com/questions/9896569/the-entity-type-list1-is-not-part-of-the-model-for-the-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!