如何处理自我跟踪实体 InvalidOperationException
STE 抛出两种类型的异常:
- UpdateException
- InvalidOperationException
通过 UpdateException,我们可以检查内部 SQLExeption.Number 来识别异常(例如 515=NullNotAllowed)
InvalidOperationException 似乎没有公开代码。 Hresult 属性受到保护。
目前我正在解析 InvalidOperationException.Message ,这很丑陋:
Try
Using ctx AS New MyEntities
ctx.Orders.ApplyChanges(order)
ctx.SaveChanges()
End Using
Catch ex As InvalidOperationException When ex.Message.Contains("...foreign-key properties is non-nullable")
Throw New FaultException("...")
Catch ex As UpdateException When CType(ex.InnerException, SqlException).Number = SQLErrorNumbers.NullNotAllowed
Throw New FaultException("...")
End Try
我们应该如何区分 InvalidOperationExceptions? 是否有可能的 InvalidOperationExceptions 列表? 可以访问受保护的 HResult 吗?
编辑 不,我不是在谈论“重复条目”InvalidOperationExceptions! 我收到 InvalidOperationException“无法更改关系,因为一个或多个外键属性不可为空...”。
STEs throw two types of Exception:
- UpdateException
- InvalidOperationException
With UpdateException we can check the inner SQLExeption.Number to identify the the Exception (ex. 515=NullNotAllowed)
InvalidOperationException seems not to expose a code. The Hresult property is protected.
Currently I am parsing InvalidOperationException.Message which is ugly:
Try
Using ctx AS New MyEntities
ctx.Orders.ApplyChanges(order)
ctx.SaveChanges()
End Using
Catch ex As InvalidOperationException When ex.Message.Contains("...foreign-key properties is non-nullable")
Throw New FaultException("...")
Catch ex As UpdateException When CType(ex.InnerException, SqlException).Number = SQLErrorNumbers.NullNotAllowed
Throw New FaultException("...")
End Try
How are we supposed to differentiate InvalidOperationExceptions?
Is there a list of possible InvalidOperationExceptions?
Can one access the protected HResult?
EDIT
No I am not talking about the "duplicate entries" InvalidOperationExceptions!
I am getting an InvalidOperationException "The relationship could not be changed because one or more of the foreign-key properties is non-nullable...".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎没有办法绕过 STE 丑陋的异常处理。
There seems to be no way around the ugly exception handling with STE.