asp.net mvc - Add child object to tables When parent object saved into context -


i have upgraded entityframework objectcontext dbcontext latest version v6.1.3 mvc webapplication. here used database first approach

have scenario add order process in database using edmx. below code behavior added objects in each child table when save parent table object context. worked in objectcontext.[every table has new entry[order,orderdetail,license]

but after upgraded dbcontext, below code added entry parent table[order]. child tables has empty record. here have more 10 child tables order process. mentioned few example. please suggest way solve issue .

table

order -parent table orderdetail -child table of order license- child table of order 

code

 using (dbentities contextentity = new dbentities ())                 {  using (transactionscope transaction = new transactionscope())                         { //parent table                     orders order = new orders();                     order.customerid = 1232;                                       order.orderdate = datetime.now;  //child table                orderdetails orderdetails = new orderdetails();                orderdetails.orders = order; //linked parend table                orderdetails.productid = 1233;                orderdetails.quantity = 3;                orderdetails.unitprice = product.unitprice;  //child table  license license = new license();   license.productid = 1233;   license.customerid= 1232;                                          license.lastmodifieddate = datetime.now;   license.orders = order; // linked parent   //add parent table in context   contextentity.orders.add(order);                          contextentity.savechanges();  transaction.complete(); }      } 

when used objectcontext, entities weren't poco , derived entityobject automatically provided tracking capabilities between orders , related data (license , orderdetails) didn't have explicitly add orderdetails , license context.

but, when switched dbcontext, ef no longer able detect license , orderdetails automatically, have add them explicitly:

contextentity.orderdetails.add(orderdetails); contextentity.licenses.add(license); 

alternatively, if you'll expose relationship directly in root object (as should have, because orderdetails - being value object - shouldn't added directly context) ef will able detect dependency , wouldn't have add them explicitly:

public class orders {     // assuming each order has many lines     public virtual icollection<orderdetails> orderlines { get; set; }      // assuming each order has many licenses     public virtual icollection<license> licenses { get; set; }      // rest of order data } 

and connection:

order.orderlines.add(orderdetails); order.licenses.add(license); 

now (once saved), child objects have orders navigation property correctly pointing parent entity don't have set them manually.


Comments