如何从 ADO.NET 获取有关外键违规的正确异常
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过创建一个实现 ISQLExceptionConverter 接口的类来实现此目的。
下面是 SQL Server 的一个示例实现:
要使用它,请在 NHibernate 配置文件中定义它,如下所示:
总体而言,此功能(或多或少)没有文档记录,但您可以在 Fabio Maulo 的博客。
You can do this by creating a class that implements the
ISQLExceptionConverter
interface.Here is an example implementation for SQL Server:
To make use of this, define it in your NHibernate config file as follows:
Overall, this feature is (more or less) undocumented, but you can find some information on Fabio Maulo's blog.