المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem with Hierarchical Update with entity framework



C# Programming
10-05-2009, 12:12 PM
Hi
i've created a sample application with these characteristics :

1. I have 2 tables (Customers & Orders) in sql server database, Customers is master table and Orders is child table.
2. I have 3 forms in my app,
a. MainForm which display all customers
b. FrmCustomer which use to add new customer or edit current customer, it also display all orders of current customer.
c. FrmOrder which use to add new order or edit current order.

i want give ability to end-user to add new customer and his orders at the same time and click Ok, then my objectContext save all changes to back-end database. for example when i add new customer, FrmCustomer display me to enter new customer info, also i add some orders to his orders list, when i click OK in FrmCustomer, it should save customer and all orders Hierarchically, but it only save customer to database and not orders! can anybody help me to solve this problem ?

here is my code in MainForm to add new customer :


Customer newCust = (Customer)this.bindingSource1.AddNew();
FrmCustomer frm = new FrmCustomer(newCust,this.db);
if (frm.ShowDialog() == DialogResult.OK)
{
this.bindingSource1.Add(newCust);
db.SaveChanges(true);
}
else
{
this.bindingSource1.CancelEdit();
}

and here is my code in FrmCustomer to add new order :

Order newOrd = (Order)this.bindingSource1.AddNew();
FrmOrder frm = new FrmOrder(newOrd, this._objContext);
if (frm.ShowDialog() == DialogResult.OK)
{
this.bindingSource1.EndEdit();
}
else
{
this.bindingSource1.CancelEdit();
}

Note : db object is my global objectContext to pass to my form's constructor.

thanks in advance.