如何处理自我跟踪实体 InvalidOperationException

发布于 2024-12-08 07:34:12 字数 952 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2024-12-15 07:34:12

似乎没有办法绕过 STE 丑陋的异常处理。

There seems to be no way around the ugly exception handling with STE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文