Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从EF DbUpdateException中获取一些附加信息_C#_Entity Framework_Entity Framework 4 - Fatal编程技术网

C# 如何从EF DbUpdateException中获取一些附加信息

C# 如何从EF DbUpdateException中获取一些附加信息,c#,entity-framework,entity-framework-4,C#,Entity Framework,Entity Framework 4,我正在使用EntityFramework5并进行更新。我得到以下异常,使用SQL事件探查器,我没有看到任何发出的SQL: catch (DbUpdateException ex) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex); } 这句话的意思是: {System.Data.Entity.Infrastructure.DbUpdateException: An erro

我正在使用EntityFramework5并进行更新。我得到以下异常,使用SQL事件探查器,我没有看到任何发出的SQL:

    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}
通常我可以找到异常的线索,但在这种情况下,我在异常列表中看不到任何有用的东西。有人对我如何找到更多信息有什么建议吗?

信息:

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

我觉得听起来很准确。检查您的实体是否已将所有(必需)外键设置为有效值。

谢谢。我花了太多的时间看细节,看不出来。我会知道下次去哪里找。你知道异常堆栈中是否有一个位置,我可以从中提取此信息,以便将其发送到日志。没问题,我通过艰苦的方式学会了阅读EF异常:)如果这有助于解决你的问题,请接受此答案