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:
- Resequence the Ordinals
- Delete the record
Instead of:
- Delete the record
- 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...