views:

449

answers:

2

If I have the following Linq code:

context.Table1s.InsertOnSubmit(t);
context.Table1s.InsertOnSubmit(t2);
context.Table1s.InsertOnSubmit(t3);

context.SubmitChanges();

And I get a database error due to the 2nd insert, Linq throws an exception that there was an error. But, is there a way to find out that it was the 2nd insert that had the problem and not the 1st or 3rd?

To clarify, there are business reasons that I would expect the 2nd to fail (I am using a stored procedure to do the insert and am also doing some validation and raising an error if it fails). I want to be able to tell the user which one failed and why. I know this validation would be better done in the C# code and not in the database, but that is currently not an option.

+1  A: 

Comment out the first and third inserts to eliminate them as suspects.

My first thought is that the second insert has the same ID as the first, but it's tough to diagnose your problem without more details about the error.

Dan Goldstein
See my comments above. I am causing the insert to fail in the SP based on some conditional logic. I just want to know which one is failing.
NotDan
+1  A: 

You can specify explicitly a conflict mode like this one :

context.SubmitChanges(ConflictMode.ContinueOnConflict);

if you want to insert what is valid and not fail on the first conflict, then use the

context.ChangeConflicts

collection to find out which objects conflicted during the insertion.

Jerome Laban