DbContext SaveChanges执行语句顺序 [英] DbContext SaveChanges Order of Statement Execution

查看:388
本文介绍了DbContext SaveChanges执行语句顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,它具有一个带有Ordinal列的表的唯一索引。因此,例如,表格将包含以下列:



TableId,ID1,ID2,Ordinal



索引位于列ID1,ID2,Ordinal之间。



我遇到的问题是,当从数据库中删除记录时,我将排序序列,使它们再次顺序。我的删除功能如下所示:

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

问题是当我调用Context.SaveChanges()时,它会打破唯一索引,因为它似乎以与传递的顺序不同的顺序执行语句。例如,以下情况发生:


  1. 排序条件

  2. 删除记录

而不是:


  1. 删除记录

  2. 排序规则

这是EF的正确行为吗?如果是,是否有办法压制这种行为强制执行的顺序?



如果我没有正确解释,请让我知道..

解决方案

命令的顺序完全受到EF的控制。唯一可以影响顺序的方式是对每个操作使用单独的SaveChanges:

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

您还应该在手动创建的事务中运行该代码,以确保atomicity(=> 的TransactionScope )。但是最好的解决方案可能是使用存储过程,因为您的重新排序必须将数据库中的所有受影响的记录拉到您的应用程序,更改其顺序并将其保存回数据库一个一个。



Btw。这样做与数据库的气味。在顺序序列中有差距是什么问题?


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

解决方案

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?

这篇关于DbContext SaveChanges执行语句顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆