EF 提交提示System.Data.Entity.Infrastructure.DbUpdateException异常的解决方法

 EF 提交提示System.Data.Entity.Infrastructure.DbUpdateException异常的,catch的信息只是 ef An error occurred while updating the entries. See the inner exception for details.

通过网上找了资料,得到了解决方法,就是获取异常的内部信息

public virtual void Save()
    {

        try
        {
            _db.SaveChanges();
        }

catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
//Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
// eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
ValidationErrors.Add(ve.ErrorMessage);
//Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",ve.PropertyName, ve.ErrorMessage);
}
}
return false;
throw new Exception("Repository.Save. Db Entity Validation Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (NotSupportedException e)
{

throw new Exception("Repository.Save. Not supported Exception. Data not saved. Error: " + GetInnerException(e));
}


catch (ObjectDisposedException e)
{

throw new Exception("Repository.Save. Repository.Save. Object Disposed Exception. Data not saved. Error: " + GetInnerException(e));

}

catch (InvalidOperationException e)
{
throw new Exception("Repository.Save. Invalid Operation Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (DbUpdateConcurrencyException e)
{
throw new Exception("Repository.Save. Db Update Concurrency Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (DbUpdateException e)
{
throw new Exception("Repository.Save. Db Update Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (EntityException e)
{
throw new Exception("Repository.Save. Entity Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (DataException e)
{
throw new Exception("Repository.Save. Data Exception. Data not saved. Error: " + GetInnerException(e));
}

catch (Exception e)
{
throw new Exception("Repository.Save. General Exception. Data not saved. Error: " + GetInnerException(e));

}


    }



下面是获取内部异常的方法...非常有用

    /// <summary>
    /// This sets up the recursive function
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public static string GetInnerException(Exception e)
    {
        string innerExceptionMessage = "";
        string error = GetInnerException(e, out innerExceptionMessage);

        return error;

    }


/// <summary>
    /// This is a recursive function which recursively drills down and gets the error.
    /// </summary>
    /// <param name="e"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    private static string GetInnerException(Exception e, out string msg)
    {
        if (e.InnerException != null)
            GetInnerException(e.InnerException, out msg);
        else
            msg = e.Message;
        return msg;
    }
posted @ 2020-11-19 14:53  编程-一生坚持的事业  阅读(1146)  评论(0编辑  收藏  举报