The entity type List`1 is not part of the model for the current context
Solution 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.
Solution 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);
Related videos on Youtube
Isaac Vallee
Updated on November 27, 2020Comments
-
Isaac Vallee over 3 years
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.
-
Leniency about 12 yearsA quick semantic note, the name App_Properties.Users would imply multiple users, not one. Convention tends to be that a single object has a singular name as saying
var users = new Users()
implies a collection of people, not a single item. -
Isaac Vallee about 12 yearsYeah, thanks... I have no control of the DB and I let the improper naming of the tables propagate down into my code.
-
Leniency about 12 yearsThe db names don't have to though - it's very easy to change the mapping of your POCO to the actual database table OnModelCreating:
modelBuilder.Entity<User>().ToTable("Users")
. A simple F2 rename on the Users class will then propagate the rename across your project. Same with property names - you can custom map any of it. weblogs.asp.net/scottgu/archive/2010/07/23/…
-
-
Isaac Vallee about 12 yearsThanks.. That got rid of that error but added a "Object with same key already exists in objectstatemanager error." I was able to get help with that error from stackoverflow.com/questions/8254854/….
-
Rush.2707 over 7 yearsThanks ... this was helpful
-
Asad Naeem over 5 yearsI got an idea from your answer so I did the following: I am using code first approach with LazyLoadingEnabled = false I had a collection of actions against a status so I loaded then like that dbContext.Entry(status).Collection(c=>c.Actions).Load();
-
Asad Naeem over 5 yearsIt worked fine but issue arises when I found that there is an object which is related to each Action NextStatus so I used the following: foreach(var a in status.Actions) { dbContext.Entry(a).Reference(c=>c.nextStatsu).Load(); }