本文介紹了實體框架中的重復鍵異常?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
當我將具有給定用戶名的現有用戶插入我的數據庫時,我試圖捕捉拋出的異常.正如標題所說,我正在使用EF.當我嘗試將用戶插入 db 時引發的唯一異常是UpdateException" - 如何提取此異常以確定它是重復異常還是其他異常?
I'm trying to catch the exception thrown when I insert a already existing user with the given username into my database. As the title says then I'm using EF. The only exception that's thrown when I try to insert the user into to db is a "UpdateException" - How can I extract this exception to identify whether its a duplicate exception or something else?
推薦答案
catch (UpdateException ex)
{
SqlException innerException = ex.InnerException as SqlException;
if (innerException != null && innerException.Number == ??????)
{
// handle exception here..
}
else
{
throw;
}
}
在 ??????
處輸入與唯一約束違規相對應的正確數字(我完全不知道).
Put the correct number at ??????
that corresponds to unique constraint violation (I don't know it from the top of my head).
這篇關于實體框架中的重復鍵異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!