Say I开发者_开发百科 have a one-to-many relation between entities Customer-Order. If I delete a Customer I also want to delete all related Order's. If I try this on the client side:
foreach (Order order in cusomter.Orders)
{
Context.Orders.Remove(order);
}
Context.Customers.Remove(customer);
Context.SubmitChanges();
It calls the Customer delete domain operation on the server first, which fails because of a relational constraint in the database. How can I get it to delete the Orders first?
Enable cascading deletes on the relationship between the parent and child table in the database.
http://rudesyle.wordpress.com/2008/01/28/cascading-deletes-in-sql-server/
SubmitChanges before removing the customer, and it will fix your issue.
EDIT: You'll have to submitchanges twice.
精彩评论