首页新闻找找看学习计划

(新手求指导)EF+MSSQL,弹出“更新条目时出错。有关详细信息,请参阅内部异常。”错误

0
悬赏园豆:20 [已解决问题] 解决于 2016-12-12 11:08

单表插入一条数据,id为自增id,插入数据时报题目上面的错误,调试跟踪时,发现是在Context.SaveChanges()处便跳出异常。该表设置了外键关系,EF的edmx文件的相应的字段也设置了identity属性。请大神们指点迷津!

EF
孤独影的主页 孤独影 | 初学一级 | 园豆:1
提问于:2016-12-12 00:44
< >
分享
最佳答案
1

这种情况一般是数据库那边出了问题,你可以查看更详细的异常信息就明白了


收获园豆:20
Jeffcky | 老鸟四级 |园豆:2549 | 2016-12-12 08:31

请问该如何查看那个异常信息,我用的是VS2015 Update3的版本

孤独影 | 园豆:1 (初学一级) | 2016-12-12 09:36

@孤独影: 你把SaveChanges保存操作放在try catch里面 出错之后再看具体excption


Jeffcky | 园豆:2549 (老鸟四级) | 2016-12-12 09:38

@JeffckyWang: 我catch后弹出一个异常信息为:

System.Data.Entity.Core.UpdateException: 更新条目时出错。有关详细信息,请参阅内部异常。 ---\u003e System.Data.SqlClient.SqlException: 将截断字符串或二进制数据。\r\n语句已终止。\r\n at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean\u0026 dataReady)\r\n at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task\u0026 task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task\u0026 task, Boolean\u0026 usedCache, Boolean asyncWrite, Boolean inRetry)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)\r\n at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.\u003cReader\u003eb__c(DbCommand t, DbCommandInterceptionContext`1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)\r\n at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)\r\n at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()\r\n --- End of inner exception stack trace ---\r\n at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.\u003cUpdate\u003eb__2(UpdateTranslator ut)\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()\r\n at System.Data.Entity.Core.Objects.ObjectContext.\u003cSaveChangesToStore\u003eb__35()\r\n at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)\r\n at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)\r\n at System.Data.Entity.Core.Objects.ObjectContext.\u003c\u003ec__DisplayClass2a.\u003cSaveChangesInternal\u003eb__27()\r\n at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)\r\n at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)\r\n at System.Data.Entity.Internal.InternalContext.SaveChanges()"

里面提到“将截断字符串或二进制数据”,度娘了一下,说数据库长度不够,但是我的字段设置都是varchar类型的,不会是长度不够问题吧

 

孤独影 | 园豆:1 (初学一级) | 2016-12-12 10:11

@孤独影: 这都说的很清楚了大哥,你觉得呢


Jeffcky | 园豆:2549 (老鸟四级) | 2016-12-12 10:12

@JeffckyWang: 哦?应该怎么看这些异常信息呢?我不知道哪条是问题的所在?

孤独影 | 园豆:1 (初学一级) | 2016-12-12 10:20

@孤独影: 字符串长度超过


Jeffcky | 园豆:2549 (老鸟四级) | 2016-12-12 10:39
其他回答(1)
0

请参阅内部异常 InnerException

花飘水流兮 | 园豆:10527 (专家六级) | 2016-12-12 08:45

请问这个InnerException是在哪里能看到的?

支持(0) 反对(0) 孤独影 | 园豆:1 (初学一级) | 2016-12-12 09:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册