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; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM