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.
-
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.
NotDan : 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. -
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.
0 comments:
Post a Comment