DbContext SaveChanges Order of Statement Execution

…衆ロ難τιáo~ 提交于 2019-11-29 13:16:53

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!