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 9086753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:33:54+00:00 2026-06-16T21:33:54+00:00

I have two tables, one table A that is the parent and other table

  • 0

I have two tables, one table A that is the parent and other table B that is the children (1:N relationship).

If I want to delete the children records in the table B when I delete the parent in the table A, if I don’t use cascade on delete in the data base, I would do it in the following way:

  1. User A load the parent record in her context, then load all the children records in the context, mark all as deleted.
  2. Other user add a new children.
  3. User A save changes. Get an error of reference integrity because the register added by the second user is not in her context.

In this case, the first user can try load all the children again when the exception is raised, but a third user can add a new children and have the same problem. In theory, this can occur all the time and the first user wouldn’t delete the parent.

For that reason, I am thinking to use a transaction to do the work.

  1. First user starts a transaction.
  2. First user mark as deleted the parent and save the changes. The parent is blocked in the database.
  3. First user load all the children, marked as deleted.
  4. First user save the changes.

In this case, I have a doubt, when the parent register is blocked by the first user, the ID of this parent can be use by other user as foreign key of a children record? Or the database does not allow to use the ID because in theory is a deleted record?

If the database does not allow to use the ID, the second user will get an exception when try to add a new children, for reference integrity, because the parent does not exists. So the problem is solved. It is not needed to do anything else.

However, if the database allows to use the ID of the blocked parent, I need to do two things:

In the method that delete the parent:

  1. Load the parent, mark it as deleted and save changes. This block the register.
  2. Load the children, mark them as deleted an save the changes.
  3. accept the transaction.

In the method that added a children

  1. Load the parent. If is blocked, the method wait until the register is free.
  2. Add the new children.
  3. Accept changes.

Really is not needed to load the parent in the context to delete the children, it would be an extra query to the database.

If the parent does not exists, get an exception an is not needed to do anything else, the user that try to added a new register now that is because the parent was deleted.

If the parent exists, the children can be added.

So I have two questions.

  1. is a good idea to use a transaction to delete a parent record to ensure that all the children are deleted?

  2. If a transaction is a good option, when the parent is blocked, the data base does not allow to use the parent ID to add a new record or I need to do the solution to load the parent in the method that delete the children?

  • 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-06-16T21:33:56+00:00Added an answer on June 16, 2026 at 9:33 pm

    Transaction does not postpone delete operation. There is nothing like “marked as deleted” in the database. When you mark entity as deleted and execute SaveChanges the record is deleted in the database. It always happens in transaction. Once the record is about to be deleted FK constrains are checked and if any dependent record exists the error is thrown (unless ON DELETE rule for the constrain doesn’t define other option). You must always delete children prior to parent when using FK constraints.

    If your description is exact you may simply use the very first approach you are afraid of and close queries and SaveChanges into single TransactionScope. It will run transaction with serializable isolation level which should ensure that no other transaction will be able to insert record into locked key range (locked by the query) until current transaction completes.

    While the solution with serializable transaction may work it can bring other issues – deadlocks with other transactions in your application. Because of that repeating failed transaction is often the best option. If your system has such a big concurrent load that delete transaction can fail “all the time” it is probably time to completely redesign your application.

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

Sidebar

Related Questions

I want to delete multiple records at once. I have two tables, one that
I have two tables tabData and tabDataDetail. I want all idData(PK) from Parent-Table(tabData) that
I have two tables one being the parent of the other. The child table
I was making one table that have two foreign key to another tables. But
I have two tables, one that has 450 rows, the other is a Sort
I have two tables, one that has a foreign key from the other. I
I have two tables : one table it matchs and the other is teams.
I have two tables, one has about 1500 records and the other has about
Let's say I have two tables with a one-to-many relationship. I want to create
I have two tables that link together through an id one is submit_moderate and

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.