2013-08-14 2 views
1

Entity Framework 5를 사용하고 업데이트하고 있습니다. 나는 예외를 다음과 내가 발행 한 SQL이 표시되지 않는 SQL 프로파일을 사용하여 얻을 :EF DbUpdateException에서 추가 정보를 얻으려면 어떻게해야합니까?

catch (DbUpdateException ex) 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex); 
    } 

이 가진 메시지는 말한다 :

{System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_QuestionQuestionStatus". The conflict occurred in database "TestDb", table "dbo.QuestionStatus", column 'QuestionStatusId'. 
The statement has been terminated. 
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
    at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 
    at System.Data.SqlClient.SqlDataReader.get_MetaData() 
    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) 
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
    at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
    at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) 
    at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) 
    --- End of inner exception stack trace --- 
    at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) 
    at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) 
    at System.Data.Entity.Internal.InternalContext.SaveChanges() 
    --- End of inner exception stack trace --- 
    at System.Data.Entity.Internal.InternalContext.SaveChanges() 
    at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 
    at System.Data.Entity.DbContext.SaveChanges() 
    at TestDb.Models.Contexts.TestDbContext.SaveChanges() in c:\K\ST134 Aug 14\TestDb.Models\Contexts\TestDbContext.cs:line 101 
    at TestDb.Services.TestDbUowBase.Commit() in c:\K\ST135 Aug 15\TestDb.Services\TestDbUowBase.cs:line 48 
    at TestDb.Web.Controllers.ProblemController.PostProblem(Problem problem) in c:\K\ST135 Aug 15\WebUx\Controllers\ProblemController.cs:line 99} 

는 보통 I는 제외되지만에서 단서를 찾을 수 있습니다 이 경우 나는 예외 목록에서 유용한 것을 볼 수 없다. 누구든지 더 많은 정보를 찾을 수있는 방법에 대한 제안이 있습니까?

답변

8

메시지 :

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_QuestionQuestionStatus". The conflict occurred in database "TestDb", table "dbo.QuestionStatus", column 'QuestionStatusId'. 

내가 생각하는 매우 정확한 소리. 엔티티가 모든 (필수) 외래 키를 유효한 값으로 설정했는지 확인하십시오.

+0

감사합니다. 나는 세부 사항을 보면서 너무 많은 시간을 보냈다. 나는 다음 번에 어디를보아야하는지 알 것이다. 예외 스택에이 정보를 추출 할 수있는 곳이 있는지 알고 계시므로 로그로 보낼 수 있습니다. – Alan2

+0

문제는 없지만 EF 예외는 어려운 방법으로 읽는 법을 배웠습니다 :) 문제를 해결하는 데 도움이된다면이 대답을 수락하십시오. –

+0

예, 동의합니다. 다시 한 번 감사드립니다. – Alan2

관련 문제