O'Reilly Forums: Problem Setting Child Objects State To Added - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem Setting Child Objects State To Added

#1 User is offline   MickySmig 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 17-February 11

Posted 04 April 2012 - 09:22 AM

I'm having a problem with adding a chlld object and I was wondering if anyone would have some advice. What I'm trying to do is take an existing Order entity that has two existing child entities and add a third one, whilst checking the others for modification. Below is the code:

[HttpPost]
public ActionResult Edit(Order order)
{
db.Entry(order).State = EntityState.Added;

if (ModelState.IsValid)
{


foreach (var orderDetail in order.OrderDetails)
{

if (orderDetail.OrderId == 0)
{
db.Entry(orderDetail).State = EntityState.Added;
}
else
{
db.Entry(orderDetail).State = EntityState.Modified;
}

// The example order that I'm updating has two child entities
// so this orderId will be for the third, added one.
int addedOrderDetailId = order.OrderDetails[2].OrderId;
}
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "CompanyName", order.CustomerId);
return View(order);
}

On the face of it, this code is the same as the example on page 90 of Entity Framework:DbContext, but this code doesn't work. What happens is that the added OrderDetail entity has an OrderId of 0 when the foreach loop is entered, but entity framework updates this OrderId after the first iteration of the loop (OrderId is set to 10 in my example). This means that the code to set the added Entity's state is not called, but the code to set the staet to modified is (as the OrderId is now set to 10). This, in turn, then causes SaveChanges() to try and update a row in the database that deson't exist.

Any help would be greatly appreciated.
0

#2 User is offline   MickySmig 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 17-February 11

Posted 05 April 2012 - 08:02 AM

Hi,

I found a solution after gaining help from another site and I thought that I'd post it here

[HttpPost]
public ActionResult Edit(Order order)
{
if (ModelState.IsValid)
{
foreach (var orderDetail in order.OrderDetails)
{

if (orderDetail.OrderId == 0)
{
db.Entry(orderDetail).State = EntityState.Added;
orderDetail.OrderId = order.OrderId;
}
else
{
db.Entry(orderDetail).State = EntityState.Modified;
}
}
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "CompanyName", order.CustomerId);
return View(order);
}

What I did was to paint the state of the child objects first and then the parent object afterwards. I don't know if this is the 'proper' way to do it, but it certainly did the trick
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users