gpt4 book ai didi

c# - Entity Framework SaveChanges 错误详细信息

转载 作者:IT王子 更新时间:2023-10-29 04:31:37 26 4
gpt4 key购买 nike

在数据上下文中使用 SaveChanges 保存更改时,有没有办法确定哪个实体导致错误?例如,有时我会忘记将日期分配给不可为空的日期字段并得到“无效日期范围”错误,但我没有得到关于它是由哪个实体或哪个字段引起的信息(我通常可以通过以下方式追踪它煞费苦心地遍历我所有的对象,但这非常耗时)。堆栈跟踪非常无用,因为它只向我显示了 SaveChanges 调用中的错误,而没有任何关于错误发生位置的额外信息。

请注意,我并不是要解决我现在遇到的任何特定问题,我只是想大致了解一下是否有办法判断是哪个实体/字段导致了问题。


以堆栈跟踪的快速示例为例 - 在这种情况下发生错误是因为 CreatedOn 日期未在 IAComment 实体上设置,但是无法从中判断错误/堆栈跟踪

    [SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.]
System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value) +2127345
System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value) +232
System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb) +46
System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Byte scale, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj) +4997789
System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc) +6248
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) +8084396
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +267

[UpdateException: An error occurred while updating the entries. See the inner exception for details.]
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +389
System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) +163
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +609
IADAL.IAController.Save(IAHeader head) in C:\Projects\IA\IADAL\IAController.cs:61
IA.IAForm.saveForm(Boolean validate) in C:\Projects\IA\IA\IAForm.aspx.cs:198
IA.IAForm.advance_Click(Object sender, EventArgs e) in C:\Projects\IA\IA\IAForm.aspx.cs:287
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5019

最佳答案

一个选项是处理 ObjectContext.SavingChanges Event ,这使您有机会在保存更改之前对实体执行验证,甚至可以在必要时取消保存。通过这种方式,您可以确保在尝试保存更改之前设置任何不可为 null 的属性,并避免必须依赖异常处理。

关于c# - Entity Framework SaveChanges 错误详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2381500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com