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
  • This topic is locked

Problem Setting Child Objects State To Added

#1 User is offline   MickySmig 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • 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: 5
  • 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

#3 User is offline   LukeWard2013 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 14-August 13

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
0

#4 User is offline   MickySmig 

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

Posted 14 August 2013 - 09:10 AM

View PostLukeWard2013, on 14 August 2013 - 05:41 AM, said:

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.
0

#5 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 02 December 2013 - 05:56 PM

closing because it's' been resolved
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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