views:

36

answers:

1

I'm updating a set of objects, but the update fails on a SqlException that says "Incorrect Syntax near 'Where'".

So I crack open SqlProfiler, and here is the generated SQL:

exec sp_executesql N'UPDATE [dbo].[Addresses]
SET 
WHERE ([AddressID] = @p0) AND ([StreetAddress] = @p1) AND ([StreetAddress2] = @p2) AND     ([City] = @p3) AND ([State] = @p4) AND ([ZipCode] = @p5) AND ([CoordinateID] = @p6) AND ([CoordinateSourceID] IS NULL) AND ([CreatedDate] = @p7) AND ([Country] = @p8) AND (NOT ([IsDeleted] = 1)) AND (NOT ([IsNonSACOGZip] = 1))',N'@p0 uniqueidentifier,@p1 varchar(15),@p2 varchar(8000),@p3 varchar(10),@p4 varchar(2),@p5 varchar(5),@p6 uniqueidentifier,@p7 datetime,@p8 varchar(2)',@p0='92550F32-D921-4B71-9622-6F1EC6123FB1',@p1='125 Main Street',@p2='',@p3='Sacramento',@p4='CA',@p5='95864',@p6='725E7939-AEE3-4EF9-A033-7507579B69DF',@p7='2010-06-15 14:07:51.0100000',@p8='US'

Sure enough, no set statement.

I also called context.GetChangeSet() and the proper values are in the updates section.

Also, I checked the .dbml file and all of the properties Update Check values are 'Always'.

I am completely baffled on this one, any help out there?

+1  A: 

I had overriden GetHashCode to return a concatenation of a few of the fields. When I changed it to return solely the hash of the primary key, it worked.

The root cause is that the updates will fail for an object whose hash code changes during its lifecycle, so when you override GetHashCode you need to pick attributes that cannot be updated, like the primary key,

KevDog
I suppose if nothing else, mark this as an answer for both those that want to answer questions and for those looking for a solution.
Marc