2016-04-28 58 views
0

有時我調用SaveChanges時有一個EntityFramework異常。 我看到這樣的消息:「更新條目時發生錯誤,詳情請參閱內部例外。」 我已經記錄了堆棧跟蹤,內部異常和東西,但沒有明確的問題解釋。我希望看到真正的查詢(它是一個mysql數據庫)和參數。你知道我如何看到或記錄真正的查詢嗎?EntityFramework異常:我如何看到真正的查詢

感謝

回答

0

您可以使用DbEntityValidationException處理程序,這將讓你知道什麼是錯的精確。

try{ 

    //Your code here 
} 

catch (DbEntityValidationException ex) 
{ 
    var errorMessages = ex.EntityValidationErrors 
    .SelectMany(x => x.ValidationErrors) 
    .Select(x => x.ErrorMessage); 
    var fullMessageError = string.Join("; ", errorMessages); 
    var exceptionMessage = string.Concat(ex.Message, "Exact Message " + fullMessageError); 


} 
catch (Exception ex) 
{ 
//General Exception here 
} 
+0

謝謝,但DbEntityValidationException不解僱 – testpresta

0

您可以設置dbContext.Database的log屬性並記錄EF生成的實際查詢。

using (var context = new MyDBContext()) 
{ 
    context.Database.Log = Console.Write; // This is where you setup where to log queries 

    // Your code here... 
} 

有MSDN上詳細的文檔https://msdn.microsoft.com/en-us/data/dn469464.aspx