Jump to content


Photo

Problem Setting Child Objects State To Added


  • This topic is locked This topic is locked
4 replies to this topic

#1 MickySmig

MickySmig

    New Member

  • Members
  • Pip
  • 5 posts

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.

#2 MickySmig

MickySmig

    New Member

  • Members
  • Pip
  • 5 posts

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

#3 LukeWard2013

LukeWard2013

    New Member

  • Members
  • Pip
  • 1 posts

Posted 14 August 2013 - 05:41 AM

Hi

Thanks for the post, I used the same approach, using jQuery to add delete rows whilst maintaining indexes on the client etc. Not sure if it's the best idea though, do you know if there's a better way with MVC 4?

Cheers

Luke

#4 MickySmig

MickySmig

    New Member

  • Members
  • Pip
  • 5 posts

Posted 14 August 2013 - 09:10 AM

Hi

Thanks for the post, I used the same approach, using jQuery to add delete rows whilst maintaining indexes on the client etc. Not sure if it's the best idea though, do you know if there's a better way with MVC 4?

Cheers

Luke


For the view I used this from Steve Sanderson:

http://blog.stevensa...et-mvc-2-style/

which I think is really the same way that you're currently doing it. I was thinking about trying to go a completely different route and store the order within session state and make the changes individually, passing the completed order and calling SaveChanges() at the end, but never got around to it.

#5 JulieLerman

JulieLerman

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 334 posts

Posted 02 December 2013 - 05:56 PM

closing because it's' been resolved




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users