添加一批实体。调用SaveChanges()

时间:2017-09-08 09:42:04

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

我有一个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次。或者说:每次将Keuring列表添加到DbContext时,68个新的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);
        }

添加的第一个检查是简单检查以查看数据库中是否已存在某个项目。我将不得不为我添加到db的每个实体执行这些检查。我更喜欢在没有检查的情况下添加它们,在调用SaveChanges时捕获异常并告诉用户出了什么问题。通过我的应用程序节省了大量的检查。我知道我无法检查每种情况,这就是为什么DbResult类也有DefaultErrorMessage属性。如果我当时“嘲笑”1个实体,这一切都可行。一次添加多个实体时问题就开始了。关于如何改进我的代码的任何建议,以便我可以找出出错的地方?理想情况下之后调用SaveChanges()。但欢迎任何其他想法!可能更改DbContext上的属性,该属性检查实体是否已存在(如果已添加到上下文中)。

2 个答案:

答案 0 :(得分:3)

如果你打电话给SaveChanges并且它将失败,那么批量中的所有操作都将被回滚。最重要的是,您将获得一个DbUpdateException,其属性Entries将包含导致错误的条目/条目。 上下文本身仍会保存您可以使用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;
        }
    }
}

答案 1 :(得分: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.";
            }
        }