LINQ to SQL context.SubmitChanges - How to get error information?

I am working on an application that immediately adds a large amount of data to an SQL database. I am using LINQ to SQL and have something similar as my insert operation:

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

Here's the problem: if I get an exception (like DuplicateKeyException), I don't have CLUE which object caused the problem. The only information I get is that at least one of the objects contains key values ​​that are identical to any other key in the database.

Is it possible to get additional information about which objects caused the conflict?

Of course, I could call SubmitChanges after each InsertOnSubmit, but with the amount of data I insert, it is incredibly slow.

Anyone have any tips? Thanks!

+4
source share
2 answers

Friend, I'm not trying to be smart alec, and maybe I still succeed, but my main assumption is that you refuse linq for use in data loads. SSIS provides simple, efficient, and easy-to-maintain ETL code. Reason, it was intended for this.

Secondly, you will not specify what type of exception will be thrown, and also if this exception, if presented to you, contains an odd internal exception. This is the first place I would look.

Good luck.

+1
source

I use Linq for sql for bulk inserts. It is slow, it is not intended for this, but it just fits well with my script.

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

Also, I use ctx.Log to get a progress bar, you may find this useful. More information is available on my blog http://epandzo.wordpress.com/2011/01/02/linq-to-sql-ctx-submitchanges-progress/

and stackoverflow question Executing LINQ to SQL SubmitChangess ()

0
source

Source: https://habr.com/ru/post/1313612/


All Articles