Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3665610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:46:54+00:00 2026-05-19T01:46:54+00:00

I am new to LINQ and EF and I have a quick question about

  • 0

I am new to LINQ and EF and I have a quick question about a forum I am developing. On this forum there are topics, and each topic has associated replies. I assumed that EF would see the constraint and when deleting the topic would also delete the associated replies. Instead it throws a constraint error. Is there an easy way to delete all associated replies without looping through them and marking each one for deletion?

For instance, 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 to do this is:

Topic topic = //LINQ to get topic.
foreach (Reply reply in topic.Replies)
{
     dbEntity.Replies.DeleteObject(reply);
}
dbEntity.Topics.DeleteObject(topic);

I guess this is fine if this is what I have to do. Just curious if there is a better way. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:46:55+00:00Added an answer on May 19, 2026 at 1:46 am

    In your database Cascade your deletes for these tables…Then when you delete the parent the database will handle the deletion of the child.

    Cascading is part of the foreign key

    Example

    If your using SQL Server

    ALTER TABLE dbo.PersonAddress ADD CONSTRAINT
        FK_PersonAddress_Address FOREIGN KEY
        (
        AddressId
        ) REFERENCES dbo.Address
        (
        AddressId
        ) ON UPDATE  NO ACTION 
         ON DELETE  CASCADE   -- Here is where you set your Update and Delete Actions for the foreign key constraint.
    

    Now basically what this says is; If i delete a person then the database will delete all addresses that are related to that person

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am somewhat new to LINQ and have a quick question regarding deleting. Say,
This should be pretty simple, but I am new at LINQ. I have a
So I have this linq query clause. var ctx = new Context(); IQueryable<Users> consulta
I have a quick linq question. I have a stored proc that should return
I have a linq expression that returns transactions in groups. Each transaction has a
I'm new to Linq and have not been able to get this working. public
I'm new to linq and linq to entities so I might have gone wrong
I am still new to LINQ, and I have wrestled with a query for
I am new to LINQ to SQL, but have done a lot of database
I'm VERY new to Linq. I have an application I wrote that is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.