Reputation: 139
I have a list which takes in two objects, I want to update the same column in each object and save it to the database. I have tried this but it doesn't work. The correct objects are in the list as I set a dropdownbox to check if they were.
List<Fighter> item = fight.Fighters.ToList<Fighter>();
item.ToList().ForEach(c => c.Draw++);
db.SaveChanges();
Any ideas?
I have a many to many relationship between fight and fighter, two fighters belong to a fight.
Edit:
[HttpPost]
public ActionResult HandleResult(Fight fight)
{
List<Fighter> item = fight.Fighters.ToList<Fighter>();
... //Code here that isnt related
foreach (var i in item)
{
item.ToList().ForEach(c => c.Draw++);
db.Fighters.Add(i);
}
db.SaveChanges();
}
So as you can see in the httpPost, I want to get the fighters that are involved in the fight and add 1 to the attribute Draw that is in the fighter entity.
Upvotes: 1
Views: 2451
Reputation: 33823
You are trying to save it without actually adding it to the database tables. It looks like you are using Entity Framework. Assuming so, you'd do something like the following.
List<Fighter> item = db.fight.Fighters.ToList<Fighter>();
item.ToList().ForEach(c => c.Draw++);
foreach (var i in item)
{
db.fight.Fighters.AddObject(i);
}
db.SaveChanges();
EDIT: Updated syntax. It wasn't correct.
Upvotes: 3
Reputation:
You have to do something like db.Add(object), before doing db.SaveChanges();
Upvotes: 0