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
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?