LINQ to SQL context.SubmitChanges - 如何获取错误详细信息?

发布于 2024-09-06 20:19:28 字数 383 浏览 2 评论 0原文

我正在开发一个应用程序,可以将大量数据一次插入到 SQL 数据库中。我使用 LINQ to SQL,并使用类似这样的插入操作:

foreach (var obj in objects) { 上下文.InsertOnSubmit(obj); } 上下文.SubmitChanges();

问题是:如果我遇到异常(例如,DuplicateKeyException),我不知道是什么对象导致了问题。我得到的唯一信息是至少有一个对象包含与数据库中其他某个键相同的键值。

是否可以提取有关导致冲突的对象的更多信息?

当然,我可以在每次 InsertOnSubmit 之后调用 SubmitChanges,但是对于我插入的数据量来说,这非常慢。

有人可以给我一些建议吗? 谢谢!

I'm working on an application where a lot of data is inserted into an SQL database at once. I use LINQ to SQL, and have something like this as my insert operation:

foreach (var obj in objects)
{
context.InsertOnSubmit(obj);
}
context.SubmitChanges();

Here's the problem: If I get an exception (for instance, DuplicateKeyException), I've NO CLUE what object caused the problem. The only information I'm getting is that at least one of the objects contains key values that are identical to some other key in the database.

Is it possible to extract more information about what object(s) caused the conflict?

Of course, I could call SubmitChanges after each and every InsertOnSubmit, but with the amount of data I'm inserting, this is incredibly slow.

Anyone have any tips for me?
Thanks!

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

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

发布评论

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

评论(2

丑丑阿 2024-09-13 20:19:28

朋友,我并不是想成为一个聪明的亚历克,也许我无论如何都会成功,但我的主要建议是你放弃 linq 以用于数据加载。 SSIS 为 ETL 工作生成简单、高效且易于维护的代码。原因是,它的设计就是为了做到这一点。

其次,您不指定抛出什么类型的异常,也不指定该异常在呈现给您时是否包含非空内部异常。这是我首先要看的地方。

祝你好运。

Friend, I'm not trying to be a smart alec, and perhaps I am succeeding anyways, but my main suggestion is that you abandon linq for use in data loads. SSIS produces simple, efficient and easy to maintain code for ETL work. Reason being, it was designed to do just that.

Secondly, you don't specify what type of exception is being thrown, nor if that exception when presented to you contains a non null inner exception. That's the first place I would look.

Good luck.

╰つ倒转 2024-09-13 20:19:28

我正在使用 Linq to sql 进行批量插入。它很慢,它不是为此设计的,但它非常适合我的场景。

我正在使用 ctx.Log 来跟踪错误。 http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

另外,我使用 ctx.Log 来获取进度条,您可能会发现这很有用。更多详细信息请参阅我的博客文章
http://epandzo.wordpress.com /2011/01/02/linq-to-sql-ctx-submitchanges-progress/

和 stackoverflow 问题
LINQ to SQL SubmitChangess() 进度

I'm using Linq to sql for bulk inserts. It's slow, It's not designed for that, but it just fits nicely with my scenario.

I'm using ctx.Log to track errors. There are nice examples on http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

Also, I'm using ctx.Log to get progress bar, you might find that usefull. More details are available on my blog post
http://epandzo.wordpress.com/2011/01/02/linq-to-sql-ctx-submitchanges-progress/

and stackoverflow question
LINQ to SQL SubmitChangess() progress

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