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.
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.
But then I got another issue. Now the Item Name cannot be displayed in the grid.
Your item is a collection named "Items", so you have to use another SqlDatasource or something else to call DataBind again to bind to the items property and then bind property of Item's Name.
Seevali
0 Points
11 Posts
EF5 + How to save an entity with its child entities at the same time to the database?
Feb 02, 2013 08:57 PM|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.
thaicarrot
Contributor
5130 Points
1464 Posts
Re: EF5 + How to save an entity with its child entities at the same time to the database?
Feb 02, 2013 09:16 PM|LINK
I guess,
First
ObservableCollection<Purchase > PurchaseMain
var purchDetail = new PurchaseDetail
{
//Field
//Child Item
Items= new Item{Field1, Field1}
}
PurchaseDetails.Add(purchDetail)
//
PurchaseMain.Add(new Purchase {
//Main fields
/Child field
PurchaseDetails.Add( purchDetail)
})
look like that
Weera
Seevali
0 Points
11 Posts
Re: EF5 + How to save an entity with its child entities at the same time to the database?
Feb 05, 2013 08:27 AM|LINK
Hi,
Thank you.
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.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF5 + How to save an entity with its child entities at the same time to the database?
Feb 08, 2013 01:04 AM|LINK
Your item is a collection named "Items", so you have to use another SqlDatasource or something else to call DataBind again to bind to the items property and then bind property of Item's Name.
Seevali
0 Points
11 Posts
Re: EF5 + How to save an entity with its child entities at the same time to the database?
Feb 08, 2013 06:51 AM|LINK
Hi, Thanks Decker.
I have already done this. But I thought that there would be a direct way of doing this.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF5 + How to save an entity with its child entities at the same time to the database?
Feb 08, 2013 06:52 AM|LINK
hard to do, because you cannot fetch a certain field's property's value unless you do data-binding and do reflecting.