两个表:
order(order_id,order_time,order_num)订单表
order_details(od_id,order_id,title,price,num)订单详情表
老的思路是:
order o=new order();
o.order_time=DateTime.Now;
o.order_num=1;
DataContext.order.InsertOnSubmit(o);
DataContext.SubmitChanges();
order_details od=new order_details();
od.title="汽车";
o.order_time=DateTime.Now;
o.order_num=1;
DataContext.order.InsertOnSubmit(o);
DataContext.SubmitChanges();
order_details od=new order_details();
od.title="汽车";
od.price="100000";
od.num=1;
od.order_id=o.order_id;
DataContext.order_details.InsertOnSubmit(ob);
DataContext.SubmitChanges();
以上是我以前用的老的方法。
最近发现了一种新的方法,可以直接添加映射!只用添加主表,子表自动就添加上去了。
当然首先要在SQL里把order_details里的order_id字段设置为外键!
order o=new order();
o.order_time=DateTime.Now;
o.order_num=2;
order_details od=new order_details();
od.title="汽车";
od.price="100000";
od.num=1;
order_details od2=new order_details();
od2.title="飞机";
od2.price="10000000";
od2.num=1;
o.order_details.Add(od);
o.order_details.Add(od2);
DataContext.order.InsertOnSubmit(o);
DataContext.SubmitChanges();
o.order_time=DateTime.Now;
o.order_num=2;
order_details od=new order_details();
od.title="汽车";
od.price="100000";
od.num=1;
order_details od2=new order_details();
od2.title="飞机";
od2.price="10000000";
od2.num=1;
o.order_details.Add(od);
o.order_details.Add(od2);
DataContext.order.InsertOnSubmit(o);
DataContext.SubmitChanges();