Anandh_Techie
Anandh_Techie

Reputation: 109

Add child object to tables When parent object saved into context

I have upgraded the EntityFramework objectContext to DBContext latest version v6.1.3 for my MVC Webapplication. Here used DataBase First approach

Have scenario to add order process in to Database using EDMX. The below code behavior will added the objects in each child table when only save the Parent table object into context. This worked perfectly in ObjectContext.[Every table has new entry[Order,OrderDetail,license]

But after upgraded to DBContext, the below code only added entry into Parent table[Order]. Child tables has empty record. Here i have more than 10 child tables for order process. Mentioned just few for example. Please suggest way to solve the 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 the parent 

//Add the parent table in to context
  contextentity.Orders.Add(order);                         
contextentity.SaveChanges();
 transaction.Complete();
}

    }

Upvotes: 4

Views: 1489

Answers (2)

monkeyhouse
monkeyhouse

Reputation: 2896

I think you have to add each entity to its table. Try adding these lines before the save changes

contextentity.OrderDetails.Add( orderDetails );
contextentity.Lisences.Add( license );

Upvotes: 1

haim770
haim770

Reputation: 49095

When you used ObjectContext, your entities probably weren't POCO and derived from EntityObject that automatically provided tracking capabilities between Orders and its related data (License and OrderDetails) so you didn't have to explicitly add orderDetails and license to the context.

But, when you switched to DbContext, EF is no longer able to detect license and orderDetails automatically, so you have to add them explicitly:

contextentity.OrderDetails.Add(orderDetails);
contextentity.Licenses.Add(license);

Alternatively, if you'll expose the relationship directly in the root object (as you should have, because orderDetails - being a value object - shouldn't be added directly to the context) EF will be able to detect the dependency and you wouldn't have to 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 your order data
}

And the connection:

order.OrderLines.Add(orderDetails);
order.Licenses.Add(license);

Now (once saved), the child objects will have their Orders navigation property correctly pointing to the parent entity as well so you don't have to set them manually.

Upvotes: 3

Related Questions