如何从 ADO.NET 获取有关外键违规的正确异常

发布于 2024-09-16 12:39:08 字数 401 浏览 1 评论 0原文

我想从 ADO.NET 获得有关外键违规的正确异常。有办法做到这一点吗?

我正在使用 try to catch ADO.Exception 并检查它的消息文本是否为“foreign”。因此,如果异常文本中存在“外来”文本,则属于违规,我可以发出警报。

这是正确的做法还是有其他方法?

try{
    base.Delete();
IList<Issue> issues = Issue.LoadForX(this);
foreach (Issue issue in issues)
{
  issue.X= null;
  issue.SaveAndCheckChanged(user);
}

}
catch(NHibernate.ADOException exception)
{...

I would like to get correct Exception from ADO.NET about foreign key violation. Is there a way to do that?

I am using try to catch ADO.Exception and check it message text for 'foreign'. So, if there is 'foreign' text in exception text, it is a violation and I can alert.

Is it the right way to do or any other method?

try{
    base.Delete();
IList<Issue> issues = Issue.LoadForX(this);
foreach (Issue issue in issues)
{
  issue.X= null;
  issue.SaveAndCheckChanged(user);
}

}
catch(NHibernate.ADOException exception)
{...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不乱于心 2024-09-23 12:39:08

您可以通过创建一个实现 ISQLExceptionConverter 接口的类来实现此目的。

下面是 SQL Server 的一个示例实现:

Public Class MsSqlExceptionConverter
    Implements ISQLExceptionConverter


    Private Enum SqlServerError As Integer
        ConstraintViolation = 2627
        ConstraintConflict = 547
    End Enum


    Public Function Convert(ByVal adoExceptionContextInfo As Global.NHibernate.Exceptions.AdoExceptionContextInfo) As System.Exception _
    Implements Global.NHibernate.Exceptions.ISQLExceptionConverter.Convert
        Dim sqle As SqlException = TryCast(ADOExceptionHelper.ExtractDbException(adoExceptionContextInfo.SqlException), SqlException)
        If sqle IsNot Nothing Then
            Select Case sqle.Number
                Case SqlServerError.ConstraintConflict
                    Return New ConstraintConflictException(InternalExceptionMessages.ConstraintConflictOccured, adoExceptionContextInfo.SqlException)
                Case SqlServerError.ConstraintViolation
                    Return New ConstraintViolationException(InternalExceptionMessages.ConstraintViolationOccured, adoExceptionContextInfo.SqlException)
            End Select
        End If
        Return SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql)
    End Function


End Class

要使用它,请在 NHibernate 配置文件中定义它,如下所示:

    <property name="sql_exception_converter">YourProduct.Infrastructure.NHibernate.ExceptionConverters.MsSqlExceptionConverter, YourProduct.Infrastructure</property>

总体而言,此功能(或多或少)没有文档记录,但您可以在 Fabio Maulo 的博客

You can do this by creating a class that implements the ISQLExceptionConverter interface.

Here is an example implementation for SQL Server:

Public Class MsSqlExceptionConverter
    Implements ISQLExceptionConverter


    Private Enum SqlServerError As Integer
        ConstraintViolation = 2627
        ConstraintConflict = 547
    End Enum


    Public Function Convert(ByVal adoExceptionContextInfo As Global.NHibernate.Exceptions.AdoExceptionContextInfo) As System.Exception _
    Implements Global.NHibernate.Exceptions.ISQLExceptionConverter.Convert
        Dim sqle As SqlException = TryCast(ADOExceptionHelper.ExtractDbException(adoExceptionContextInfo.SqlException), SqlException)
        If sqle IsNot Nothing Then
            Select Case sqle.Number
                Case SqlServerError.ConstraintConflict
                    Return New ConstraintConflictException(InternalExceptionMessages.ConstraintConflictOccured, adoExceptionContextInfo.SqlException)
                Case SqlServerError.ConstraintViolation
                    Return New ConstraintViolationException(InternalExceptionMessages.ConstraintViolationOccured, adoExceptionContextInfo.SqlException)
            End Select
        End If
        Return SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql)
    End Function


End Class

To make use of this, define it in your NHibernate config file as follows:

    <property name="sql_exception_converter">YourProduct.Infrastructure.NHibernate.ExceptionConverters.MsSqlExceptionConverter, YourProduct.Infrastructure</property>

Overall, this feature is (more or less) undocumented, but you can find some information on Fabio Maulo's blog.

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