简体   繁体   中英

ado.net entity framework delete rows

I may be completely blind and stupid, but I cannot find any method generated by ADO.NET Entity Data Model that would somehow delete rows from my table. I didn't want to create a custom query. So how do I do that? Please help.

I don't have the method DeleteOnSubmit... don't know why. This is the code I wanted to use.

var deleteOrderDetails =
from details in db.OrderDetails
where details.OrderID == 11000
select details;

foreach (var detail in deleteOrderDetails)
{
db.OrderDetails.DeleteOnSubmit(detail);
}

db.SubmitChanges();

A couple of alterations needed:

db.DeleteObject(detail);

and

db.SaveChanges();

Kindness,

Dan

PS: Have you been using Linq to SQL and then swapped to the Entity Framework?

Here's another way (thanks to this answer )

I assume you have the Orders table, and OrderDetails is related to it via OrderID .

var order = db.Orders.FirstOrDefault(o=> o.OrderID == 11000);
if(order != null)
{
  order.OrderDetails.Clear();
  db.SaveChanges();
}

This should delete all the order details associated with that order.

edit: fixed the code

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM