I am new to LINQ and EF, and I have a quick question about the forum that I am developing. This forum has topics, and each topic has related answers. I assumed that EF would see a limitation, and when deleting a topic, related answers will also be deleted. Instead, it throws a constraint error. Is there an easy way to delete all related answers without scrolling through them and mark each one for deletion?
For example, in SQL, I would just do something like this:
DELETE FROM topic_replies WHERE TopicID='999' DELETE FROM topics where TopicID='999'
But in EF, the only way I know this is:
Topic topic = //LINQ to get topic. foreach (Reply reply in topic.Replies) { dbEntity.Replies.DeleteObject(reply); } dbEntity.Topics.DeleteObject(topic);
I think itโs great if thatโs what I should do. Just curious if there is a better way. Thanks in advance.
source share