删除(Delete)1.简单形式
说明:调用DeleteOnSubmit方法即可。
OrderDetail orderDetail =
db.OrderDetails.First
(c => c.OrderID == 10255 && c.ProductID == 36);
db.OrderDetails.DeleteOnSubmit (orderDetail);
db.SubmitChanges();
语句描述:使用 DeleteOnSubmit方法从OrderDetail 表中删除OrderDetail对象。调用 SubmitChanges 将此删除保持到数据库。
2.一对多关系
说明:Order 与OrderDetail是一对多关系,首先DeleteOnSubmit其OrderDetail(多端),其次 DeleteOnSubmit其Order(一端)。因为一端是主键。
var orderDetails =
from o in db.OrderDetails
where o.Order.CustomerID == "WARTH" &&
o.Order.EmployeeID == 3
select o;
var order =
(from o in db.Orders
where o.CustomerID == "WARTH" && o.EmployeeID == 3
select o).First();
foreach (OrderDetail od in orderDetails)
{
db.OrderDetails.DeleteOnSubmit(od);
}
db.Orders.DeleteOnSubmit(order);
db.SubmitChanges();
语句描述语句描述:使用DeleteOnSubmit方法从Order 和Order Details表中删 除Order和Order Detail对象。首先从Order Details删除,然后从Orders删除。 调用SubmitChanges将此删除保持到数据库。