C# 实体类型 List`1 不是当前上下文模型的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9896569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:07:34  来源:igfitidea点击:

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

c#asp.net-mvc-3entity-framework-4.1

提问by Isaac Vallee

I've been using Database First, EF 4.1

我一直在使用数据库优先,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.

我收到“实体类型 List`1 不是当前上下文模型的一部分。” 尝试从我的编辑视图更新记录时出错。

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.

我怀疑 foreach 循环不适用于为 ICollection 中的每个项目设置 EntityState。

Any assistance would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by Leniency

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.

您正在调用db.Entry(properties),因此您试图一次附加整个集合。DbContext.Entry(object)需要单个 object,而不是集合。

回答by Jonathan

Thanks, Leniency, for the answer. Worked great.

谢谢,宽大处理,为答案。工作得很好。

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

对于它的价值,我更喜欢将我的EntityState.Modified作业保留在一行上(因为我有多个)所以使用了以下 LINQ:

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

回答by Ian Grainger

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:

尽管使用了代码优先的 EF,但我还是来到了这里。对我来说,这个问题的答案就是不要尝试将实体列表传递给 insert 方法,而是一次插入一个:

entityList.ForEach(context.Insert);

entityList.ForEach(context.Insert);

回答by malik masis

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