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

后端 未结 4 1887
长情又很酷
长情又很酷 2020-12-15 16:57

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 recor

相关标签:
4条回答
  • 2020-12-15 17:37

    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);
    
    0 讨论(0)
  • 2020-12-15 17:57

    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);
    }
    
    0 讨论(0)
  • 2020-12-15 18:01

    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.

    0 讨论(0)
  • 2020-12-15 18:01

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

    0 讨论(0)
提交回复
热议问题