【问题标题】:Adding batch of entities. How to determine which entities failed when calling SaveChanges()添加一批实体。调用 SaveChanges() 时如何确定哪些实体失败
【发布时间】:2017-09-08 09:42:04
【问题描述】:

我有一个 Tools_NawContext 类扩展 DbContext 和一个 DbResult 类,以便在发生异常时稍微调整 SaveChanges 方法的结果。当抛出异常时,我会创建一条我知道属于我尝试添加、删除或编辑的实体的特定错误消息。用户可以根据错误信息采取适当的措施,然后重试。

public partial class Tools_NawContext : DbContext
{
    public Tools_NawContext(DbContextOptions<Tools_NawContext> options) : base(options) { }

    public DbResult TrySaveChanges()
    {
        try {
            int numberOfRowsSaved = SaveChanges();
            return new DbResult(numberOfRowsSaved);
        } catch(Exception ex) {
            return new DbResult(ex);
        }
    }
}

public class DbResult
{
    public DbResult(int numberOfRowsSaved) {
        this.Succeeded = true;
        this.NumberOfRowsSaved = numberOfRowsSaved;
    }

    public DbResult(Exception exception)
    {
        this.Exception = exception;
        if(exception.GetType() == typeof(DbUpdateException) && exception.InnerException != null) {
            if (exception.InnerException.Message.StartsWith("The DELETE statement conflicted with the REFERENCE constraint")) {
                this.DuplicateKeyError = true;
                this.DuplicateKeyErrorMessage = "There are other objects related to this object. First delete all the related objects.";
            } else if (exception.InnerException.Message.StartsWith("Violation of PRIMARY KEY constraint")) {
                this.DuplicateKeyError = true;
                this.DuplicateKeyErrorMessage = "There is already a row with this key in the database.";
            } else if (exception.InnerException.Message.StartsWith("Violation of UNIQUE KEY constraint")) {
                this.DuplicateKeyError = true;
                this.DuplicateKeyErrorMessage = "There is already a row with this key in the database.";
            }
        } else if(exception.GetType() == typeof(System.InvalidOperationException) && exception.Message.StartsWith("The association between entity types")) {
            this.DuplicateKeyError = true;
            this.DuplicateKeyErrorMessage = "There are other objects related to this object. First delete all the related objects.";
        }
    }

    public bool Succeeded { get; private set; }
    public int NumberOfRowsSaved { get; private set; }
    public bool DuplicateKeyError { get; private set; }
    public string DuplicateKeyErrorMessage { get; private set; }
    public Exception Exception { get; private set; }
    public List<string> ErrorMessages { get; set; }
    public string DefaultErrorMessage { get { if (Succeeded == false) return "Er is een fout in de database opgetreden."; else return ""; } private set { } }
}

但是我现在正在尝试导入一些 JSon 并想再次使用 TrySaveChanges 方法。但是这次经过一些检查,我首先将多个实体添加到上下文中,而不仅仅是 1。一旦全部添加,我调用 TrySaveChanges 方法。它仍然有效,但如果出现故障,我无法确定哪些实体未能保存。如果我添加 1000 个实体并且只有 1 个会失败,我无法确定哪里出了问题。 如何确定哪些添加的实体引发了错误?下面是我如何使用它的示例。


我有 2 个 EF 生成的类。 TestresultatenKeuring

public partial class Testresultaten
{
    public int KeuringId { get; set; }
    public int TestId { get; set; }
    public string Resultaat { get; set; }
    public string Status { get; set; }
    public int TestinstrumentId { get; set; }

    public virtual Keuring Keuring { get; set; }
    public virtual Test Test { get; set; }
    public virtual Testinstrument Testinstrument { get; set; }
}

public partial class Keuring
{
    public Keuring()
    {
        Keuring2Werkcode = new HashSet<Keuring2Werkcode>();
        Testresultaten = new HashSet<Testresultaten>();
    }

    public int Id { get; set; }//NOTE: Auto-incremented by DB!
    public int GereedschapId { get; set; }
    public DateTime GekeurdOp { get; set; }
    public int KeuringstatusId { get; set; }
    public int TestmethodeId { get; set; }
    public DateTime GekeurdTot { get; set; }
    public string GekeurdDoor { get; set; }
    public string Notitie { get; set; }

    public virtual ICollection<Keuring2Werkcode> Keuring2Werkcode { get; set; }
    public virtual ICollection<Testresultaten> Testresultaten { get; set; }
    public virtual Gereedschap Gereedschap { get; set; }
    public virtual Keuringstatus Keuringstatus { get; set; }
    public virtual Testmethode Testmethode { get; set; }
}

我有一个_KeuringImporter 类,它有一个方法可以将newKeuringtestresultatenList 添加到dbContext(_Tools_NawContext)。

private Result<KeuringRegel, Keuring> SetupKeuringToDB2(KeuringRegel row, int rownr, Keuring newKeuring)
{
    _Tools_NawContext.Keuring.Add(newKeuring);

    List<string> errorMessages = new List<string>();
    List<Testresultaten> testresultatenList = new List<Testresultaten>();

    foreach (string testName in row.testNames.Keys.ToList())
    {
        string testValue = row.testNames[testName].ToString();
        Test test = _Tools_NawContext.Test.Include(item => item.Test2Testmethode).SingleOrDefault(item => item.Naam.Equals(testName, StringComparison.OrdinalIgnoreCase));

        //-----!!NOTE!!-----: Here KeuringId = newKeuring.Id is a random negative nr and is not beeing roundtriped to the db yet!
        Testresultaten newTestresultaten = new Testresultaten() { KeuringId = newKeuring.Id, TestId = test.Id, Resultaat = testValue, Status = row.Status, TestinstrumentId = 1 };
        testresultatenList.Add(newTestresultaten);
    }
    _Tools_NawContext.Testresultaten.AddRange(testresultatenList);

    return new Result<KeuringRegel, Keuring>(row, newKeuring, errorMessages);
}

就像我说的。我用它来导入 JSON。如果一个 JSON 文件包含 68 行,则该方法被调用 68 次。或者说:68 个新的 Keuring 项目附加到 DbContext 并且每次 Testresultaten 列表添加到 DbContext。

一切就绪后,我终于从我的控制器调用SaveSetupImportToDB。 (这个方法也是我_KeuringImporter类的一部分。)

public DbResult SaveSetupImportToDB()
{
    DbResult dbResult = _Tools_NawContext.TrySaveChanges();
    return dbResult;
}

如何实现我想要的?在上述情况下,在我的 MS SQL 数据库中,Keuring 表的主键为Id,由 db 自动递增。该表还具有组合的唯一键 GereedschapIdGekeurdOp

我可以在将 newKeuring 添加到上下文之前编写一些检查,如下所示:

private Result<KeuringRegel, Keuring> SetupKeuringToDB2(KeuringRegel row, int rownr, Keuring newKeuring)
        {
            List<string> errorMessages = new List<string>();
            var existingKeuring = _Tools_NawContext.Keuring.SingleOrDefault(x => x.Id == newKeuring.Id);
            if(existingKeuring == null) { errorMessages.Add("There is already a keuring with id "  + newKeuring.Id + " in the db."); }
            existingKeuring = _Tools_NawContext.Keuring.SingleOrDefault(x => x.GereedschapId == newKeuring.GereedschapId && x.GekeurdOp == newKeuring.GekeurdOp);
            if (existingKeuring == null) { errorMessages.Add("There is already a keuring with GereedschapId " + newKeuring.GereedschapId + " and GekeurdOp " + newKeuring.GekeurdOp + " in the db."); }
            //Some more checks to cerrect values of properties: 
            //-DateTimes are not in future
            //-Integers beeing greater then zero
            //-String lengths not beeing larger then 500 characters
            //-And so on, etc...

            _Tools_NawContext.Keuring.Add(newKeuring);

            List<Testresultaten> testresultatenList = new List<Testresultaten>();

            foreach (string testName in row.testNames.Keys.ToList())
            {
                string testValue = row.testNames[testName].ToString();
                Test test = _Tools_NawContext.Test.Include(item => item.Test2Testmethode).SingleOrDefault(item => item.Naam.Equals(testName, StringComparison.OrdinalIgnoreCase));

                //-----!!NOTE!!-----: Here KeuringId = newKeuring.Id is a random negative nr and is not beeing roundtriped to the db yet!
                Testresultaten newTestresultaten = new Testresultaten() { KeuringId = newKeuring.Id, TestId = test.Id, Resultaat = testValue, Status = row.Status, TestinstrumentId = 1 };
                testresultatenList.Add(newTestresultaten);
            }
            _Tools_NawContext.Testresultaten.AddRange(testresultatenList);

            return new Result<KeuringRegel, Keuring>(row, newKeuring, errorMessages);
        }

添加的第一个检查是检查数据库中是否已存在项目的简单检查。我必须对添加到数据库的每个实体进行这些检查。我更喜欢在不检查的情况下添加它们,在调用SaveChanges 时捕获异常并告诉用户出了什么问题。通过我的应用程序为我节省了很多检查。我知道我无法检查所有情况,这就是为什么 DbResult 类也具有 DefaultErrorMessage 属性。如果我当时“粗略”地处理 1 个实体,这一切都可以正常工作。当一次添加多个实体时,问题就开始了。关于如何改进我的代码以便找出哪里出了问题的任何建议?理想情况下调用SaveChanges()之后。但欢迎任何其他想法!可能会更改 DbContext 上的一个属性,该属性会检查一个实体是否已经存在,如果它被添加到上下文中。

【问题讨论】:

  • 有没有办法在添加单个实体时,上下文可以确定它是否已经存在于上下文中?
  • 通过上下文对象的ChangeTracker属性

标签: c# asp.net-core-mvc entity-framework-core


【解决方案1】:

如果您调用 SaveChanges 并且它会失败,那么批处理中的所有操作都将被回滚。最重要的是,您将获得一个带有属性EntriesDbUpdateException,其中包含导致错误的条目/条目。 上下文本身仍会保存您可以使用ChangeTracker.Entries() 获得的跟踪对象的状态(包括失败)(可能您不需要它)

try
{                
    model.SaveChanges();
}
catch (DbUpdateException e)
{
    //model.ChangeTracker.Entries();
    //e.Entries - Resolve errors and try again
}

你的情况,你可以做一个循环,继续尝试,直到所有的东西都被保存

while (true)
{
    try
    {
        model.SaveChanges();
        break;
    }
    catch (DbUpdateException e)
    {
        foreach (var entry in e.Entries)
        {
            // Do some logic or fix
            // or just detach
            entry.State = System.Data.Entity.EntityState.Detached;
        }
    }
}

【讨论】:

  • 根据我的经验,DbUpdateException.Entries 总是only 包含导致问题的第一个条目,因此这不适合批量。至于更改跟踪器中的对象:当然,它们仍然存在,但状态不会显示哪些对象失败了。如果添加了 100 个并且 50 个有错误,则所有实体仍处于 Added 状态。
  • 您仍然可以从异常中定义失败的对象并对其进行处理(例如,在示例代码中分离)。我同意这是唯一的第一个实体,它可以在最坏的情况下导致重复请求,因为元素被标记为要保存。所以开发者应该分析无效数据的概率并定义批次的大小。
  • 除此之外,我们还有GetValidationErrors 函数,它允许在实际调用之前预先验证我们的数据,但它不会从数据库约束中保存。或许 EF 团队改进对批处理的支持是一个很好的 CR
  • 上述所有 cmets 和答案本身似乎都适用于 EF6 - System.Data.EntityGetValidationErrors() 等。EF Core(这是 OP 问题的目标)构建并执行 SQL 批处理,所以虽然有DbUpdateException.Entries,但在我的测试中它总是为空
  • @ASpirin EFC 中没有GetValidationErrors() 方法(和验证)。
【解决方案2】:

原因是:

当您向数据库添加多个条目时,您会创建一个列表,或者在 JSON 的情况下创建一个数据数组。

如您所料,您将收到第一个元素。

你需要做什么:

为错误消息创建一个数组,并将异常推送到数组中。

然后查询数组并检查数组是否有任何消息,我也会考虑字典列表而不是数组,这样您就可以为每个条目设置一个固定键,这样您就可以跟踪哪个条目有问题。

所以你会有一个看起来像这样的方法:

     public DbResult(Exception exception, ref List<string> exceptionArray)
        {
            this.Exception = exception;
            if(exception.GetType() == typeof(DbUpdateException) && exception.InnerException != null) {
                if (exception.InnerException.Message.StartsWith("The DELETE statement conflicted with the REFERENCE constraint")) {
                    this.DuplicateKeyError = true;
                    this.DuplicateKeyErrorMessage = "There are other objects related to this object. First delete all the related objects.";
exceptionArray.Add(this.DuplicateKeyErrorMessage);
                } else if (exception.InnerException.Message.StartsWith("Violation of PRIMARY KEY constraint")) {
                    this.DuplicateKeyError = true;
                    this.DuplicateKeyErrorMessage = "There is already a row with this key in the database.";
                } else if (exception.InnerException.Message.StartsWith("Violation of UNIQUE KEY constraint")) {
                    this.DuplicateKeyError = true;
                    this.DuplicateKeyErrorMessage = "There is already a row with this key in the database.";
                }
            } else if(exception.GetType() == typeof(System.InvalidOperationException) && exception.Message.StartsWith("The association between entity types")) {
                this.DuplicateKeyError = true;
                this.DuplicateKeyErrorMessage = "There are other objects related to this object. First delete all the related objects.";
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多