Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL "1 of 2 Updates failed" on "SubmitChanges()"

I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error

Example Code

X objX = _context.X.ToList().Where(x => x.DeletedOn == null).First();
objX.DeletedOn = DateTime.Now;

EntitySet<Y> objYs = objX.Ys;
Y objY = objYs[0];
objY.DeletedOn = DateTime.Now;

_context.SubmitChanges();

On SubmitChanges() I get an exception "1 of 2 Updates failed", no other information as to why that happened. Any ideas?

Also the exception type is ChangeConflictException

like image 311
soldieraman Avatar asked Dec 14 '09 06:12

soldieraman


1 Answers

Sooo what was the cause of the problem - A trigger

I did a sql profiler and saw that

When ObjY's DeletedOn property got updated a trigger updated ObjX's property (value in table) called CountOfX

which led to an error as the SQL created by LINQ to SQL had the old CountOfX value in it.

Hence the conflict.

If you ever get this error - SQL profiler is the best place to start your investigation

ALSO NOT RELATED TO THE QUESTION I am testing LINQ to SQL and ADO.net Framework, weirdly this error happened in LINQ to SQL but not in ADO.net framework. But I like LINQ to SQL for its Lazy Loading. Waiting for EF to get outta beta

like image 137
soldieraman Avatar answered Sep 18 '22 08:09

soldieraman