7

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();

2 Answers 2

10

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes. That would be the case. Also - can I delete multiple rows at once by this?
You can do mutiple delete objects per SaveChanges, yes :)
No, what I meant was if I could do dv.DeleteObject(deleteOrderDetails);??
make a list of objects you can remove and then iterate appyling the Contetxt.Model.Remove(item); when finish your iteration simply apply Context.SaveChanges and should works
6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.