LINQ to SQL UPDATE SET WHERE 0 = 1 WTF?!
Tuesday, December 29, 2009 by Administrator
Ok, just a quick one...this had me stumped for over an hour!
I wanted to simply update one column on a "user" object in the
database. So, I retrieved the object from the DB, set the new value
and called SubmitChanges. I then get hit with a
ChangeConflictException.
It was a fresh DataContext and the property in the DBML file
looked OK. I then checked the output of the SQL and it looked
something like:
UPDATE User
SET CalendarID = @p0
WHERE 0 = 1
What was the 0 = 1 where clause all about? As it turns out I
think its meant to be some kind of LINQ to SQL optimisation gone
wrong...anway this started to lead me down the right path and I
ended finding out that it was another "user" property in the DBML
file that was causing the problem. I had a column name
"DisplayName" which was set as NULL in the DB but not nullable in
the DBML file. Fixing this seems to have solved the problem...even
though the DisplayName property had nothing to do with the
UPDATE.
Want to Leave a comment?