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
{
[Key]
public string TrNo { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public int SupplierID { get; set; }
public ObservableCollection<PurchaseDetail> PurchaseDetails { get; set; }
}
and
public class PurchaseDetail
{
[Key, Column(Order = 0)]
public string TrNo { get; set; }
[Key, Column(Order = 1)]
public int ItemID { get; set; }
[Required]
public double Qty { get; set; }
[Required]
public decimal CostPrice { get; set; }
[Required]
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.