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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:46:15+00:00 2026-05-25T10:46:15+00:00

I have a table that has a unique index on a table with an

  • 0

I have a table that has a unique index on a table with an Ordinal column. So for example the table will have the following columns:

TableId, ID1, ID2, Ordinal

The unique index is across the columns ID1, ID2, Ordinal.

The problem I have is that when deleting a record from the database I then resequence the ordinals so that they are sequential again. My delete function will look like this:

    public void Delete(int id)
    {
        var tableObject = Context.TableObject.Find(id);
        Context.TableObject.Remove(tableObject);
        ResequenceOrdinalsAfterDelete(tableObject);
    }

The issue is that when I call Context.SaveChanges() it breaks the unique index as it seems to execute the statements in a different order than they were passed. For example the following happens:

  1. Resequence the Ordinals
  2. Delete the record

Instead of:

  1. Delete the record
  2. Resequence the Ordinals

Is this the correct behaviour of EF? And if it is, is there a way of overriding this behaviour to force the order of execution?

If I haven’t explained this properly, please let me know…

  • 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-25T10:46:16+00:00Added an answer on May 25, 2026 at 10:46 am

    Order of commands is completely under control of EF. The only way how you can affect the order is using separate SaveChanges for every operation:

    public void Delete(int id)
    {
        var tableObject = Context.TableObject.Find(id);
        Context.TableObject.Remove(tableObject);
        Context.SaveChanges();
        ResequenceOrdinalsAfterDelete(tableObject);
        Context.SaveChanges();
    }
    

    You should also run that code in manually created transaction to ensure atomicity (=> TransactionScope).

    But the best solution would probably be using stored procedure because your resequencing have to pull all affected records from the database to your application, change their ordinal and save them back to the database one by one.

    Btw. doing this with database smells. What is the problem with having a gap in your “ordinal” sequence?

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

Sidebar

Related Questions

I have a database table that has a Unique Key constraint defined to avoid
I have a table that has a processed_timestamp column -- if a record has
I have a table that has three different date columns, so I set each
I have a table in my schema that has a unique constraint on two
I have a table that is partitioned by TRANSACTION_DATE_TIME. Table has a column: ID.
We have a MyISAM table with approximately 75 milion rows that has 5 columns:
I have a user table that has many columns, it looks roughly like this:
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many
I have a table that has redundant data and I'm trying to identify all
I have a table that has an insert trigger on it. If I insert

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.