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