EF5 + How to save an entity with its child entities at the same time to the database?

Topics: General
Feb 2, 2013 at 9:04 PM
EF5 + How to save an entity with its child entities at the same time to the database?

3 minutes ago|LINK

I have two entities in my project (actually three with the item entity).
public class Purchase
    public string TrNo { get; set; }
    public DateTime Date { get; set; }
    public int SupplierID { get; set; }

    public ObservableCollection<PurchaseDetail> PurchaseDetails { get; set; }
public class PurchaseDetail
    [Key, Column(Order = 0)]
    public string TrNo { get; set; }
    [Key, Column(Order = 1)]
    public int ItemID { get; set; }
    public double Qty { get; set; }
    public decimal CostPrice { get; set; }
    public decimal TotalCost { get; set; }

    public virtual Purchase Purchases { get; set; }
    public virtual Item Items { get; set; }
In the Purchase form, when user adding item s to a grid, I'm creating an

ObservableCollection<PurchaseDetail> PurchaseDetails with added items.

And, finally a Purchase entity.

When the user clicks on the Save button, I want to save the item details (purchased) into the PurchaseDetails table and main data into the Purchases table.

Please help me to do this job in a right (standard) way.
Feb 4, 2013 at 5:19 PM
Hi seevali,

If all the entities are being tracked by a context, then calling SaveChanges on that context will save all the entities. if this is not working for you can you provide additional information on what errors/behaviors you are seeing?

Feb 4, 2013 at 6:47 PM
Edited Feb 4, 2013 at 6:51 PM
In entity framework we meet to Tracking meens that any changes on entities is clear for Context and any entities has entity state "EntityState". so, then you add object with all children to the your context , you should have sample below code :
using(MydbEntities context=new MydbEntities())
  //       this line can help you for web business code: 
   Purchase entity=new Purchase();
   entity= /* "your Purchase Entity that filled and have children" */  YourPurchaseEntity ; 
   context.AddToPurchase( entity);     
  context.SaveChanges(); //---end line Of block       
notice : if you develop Web App cause "Post Back" you should in business class Create New Instance Of Your Context before any CUD operations and Select .
also in Win App , the context managed tracking of any changes on entities and you for Debug , you can check EntityState Of any object in Using Block Code "Context".
and one of best advantages EF that is understand relation between master and Detail Entities When you insert to DB or other Operations , by Transactional worked ,by writing context.SaveChanges();
and after any changes context return any affect to entities in Block code , and EntityState of entities changed.
Feb 5, 2013 at 8:22 AM
Thank you @Arthur and @sarshogh_eng,

Sorry for the late.

I just solved my problem by doing a simple change in my code.

I mentioned in my question that I'm using a ObservableCollection<PurchaseDetail> PurchaseDetails object for creating a PurchaseDetails object collection that the user added to the grid. There was the error; when creating a PurchaseDetails object, I also added the Item object to that. My plan is to display the Item Name in the grid (Because the PurchaseDetails object doesn't have the ItemName property.) It was the error. When I stoped adding that Item , the SaveChanges() worked nicely.

But then I got another issue. Now the Item Name cannot be displayed in the grid.

Is there any wrong implementation that I did? Please help me.