How to insert data in trasaction using entity-framewrok-4?
I have a order object and it has other collection that i need to inset in database in transaction.
cOrder->
->lstOrderItems
-->lstDressing, lstTopping, lstspecialInstruction
->userDetails
->trasactionTblsdetails
Also opinion required:- 1) Is it good to maintain transaction at database level or entity framework level. 2) What should be coding style.
right now i am following it this way but as i can predict it would require a lot of code but want to know this is only the way it work or far better solution exist.
public static void SaveOrder()
{
using (EposWebOrderEntities Ctx = new EposWebOrderEntities())
{
using (TransactionScope scope = new TransactionScope())
{
// do something...
Ctx.order.SaveChanges();
// do something...
//foreach to save the order items to databaswe
Ctx.orderitems.SaveChanges();
// do something...
//foreach to save in dressing tbl
Ctx.dressing.SaveChanges();
//foreach to save in topping tbl
Ctx.topping.SaveChanges();
//foreach to save in dressing tbl
Ctx.dressing.SaveChanges();
//foreach to save in spinst tbl
Ctx.dressing.SaveChanges();
scope.Complete();
success = true;
}
}
}