how to insert data into one-to-many relationship using linq to sql. i have two table first is person and secondly is order ,i have make a relationship between both table,i want to insert data into both table at a time
public ActionResult Add(CustomerViewData data)
{
string message = "";
var item = data.Customer;
try
{
_repositoryCustomer.Insert(item);
_repositoryCustomer.Save();
return RedirectToAction("Edit", new { message = Constants.RecordSaved, id = item.CustomerId});
}
catch (Exception ex)
{
message = "Customer had error saving.";
Log.Error(message, ex);
}
return View("Edit", new CustomerViewData { PageMode = PageMode.Add, Message = message });
}
using (MyDataContext dc = new MyDataContext())
{
Person p = new Person();
Order o = new Order();
p.Orders.Add(o); //connect the objects into an object graph.
dc.Persons.Add(p); //add the graph to the data context
dc.SubmitChanges(); //save it all.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.