如何使 Linq to Sql 删除异常消息更加用户友好

发布于 2024-10-07 06:28:51 字数 746 浏览 0 评论 0原文

我想告诉用户记录无法删除的确切原因,我有这段代码来执行删除:

try{
    var table = Context.GetTable<TRecordType>();
    lock (table) {
                    table.DeleteOnSubmit(recordToDelete);
                    Context.SubmitChanges();
                 }                
    catch (Exception ex)  {
                //Put back record                  
                throw new Exception("Could not perform dataservice delete operation", ex);
            }

正如你所看到的,非常简单,但通常由于外键约束,记录无法删除,所以我得到此 SQLException 并显示如下消息:“DELETE 语句与 REFERENCE 约束“FK_Something”冲突...冲突发生在数据库“X”、表“dbo.Department”、列“DepartmentId”中。现在我真正想要的是do 至少通知用户他无法删除该记录,因为异常中提到的该表正在引用我讨厌必须解析文本才能做到这一点,这是唯一的方法吗?可以获得对打破约束的记录的引用,这样我就可以告诉用户“你不能删除文档 A,因为文档 B、C 和 D 正在引用它”。

I would like to inform the user the exact reason why a record could not be deleted, I have this code to perform deletions:

try{
    var table = Context.GetTable<TRecordType>();
    lock (table) {
                    table.DeleteOnSubmit(recordToDelete);
                    Context.SubmitChanges();
                 }                
    catch (Exception ex)  {
                //Put back record                  
                throw new Exception("Could not perform dataservice delete operation", ex);
            }

As you can see is pretty simple, but usually the record cannot be deleted because of a foreign key constraint, so I get this SQLException with a message like: "The DELETE statement conflicted with the REFERENCE constraint "FK_Something"... The conflict occurred in database "X", table "dbo.Department", column 'DepartmentId. Now what I would really like to do is atleast inform the user that he cannot delete the record because is being referenced by this table mentioned on the exception. I'd hate having to parse text to do that, is this the only way? Also it would be really nice if I could get a reference to the record breaking the constraint so I can tell the user "you can't delete document A, as is being referenced by documents B,C and D".

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

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

发布评论

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

评论(1

孤凫 2024-10-14 06:28:51

首先专门捕获SQLException,因为这就是您正在处理的内容。

其次,检查以确保异常是您“期望的”异常。您还需要处理其他 SQLException,或者重新抛出它们。

您必须解析该文本(排序——只需使用 RegEx),因为这是一条从 SQL Server 返回的消息。它引用表名和数据库约束,而不是对象或应用程序知道的任何其他内容。

当然,您确实在 recordToDelete 变量中引用了无法删除的记录。

您还拥有 LINQ-to-SQL 模型,因此您可以遍历该记录的关系,以便识别引用它的文档。

Start by catching the SQLException specifically, since that is what you are handling.

Second, check to make sure the exception is the one you "expect." You'll need to either handle other SQLExceptions as well, or re-throw them.

You are going to have to parse the text (sort off -- just use RegEx) because this is a message that has been returned from SQL Server. It references table names and database constraints, not objects or anything else that your application knows about.

Of course, you do have a reference to the record that couldn't be deleted, in the recordToDelete variable.

You also have your LINQ-to-SQL model, so you could potentially traverse the relationships to that record in order to identify the documents that reference it.

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