I’m having some troubles with updating linq to sql entities.
For some reason, I can update every single field of my item entity besides name.
Here are two simple tests I wrote:
[TestMethod]
public void TestUpdateName( ) {
using ( var context = new SimoneDataContext( ) ) {
Item item = context.Items.First( );
if ( item != null ) {
item.Name = "My New Name";
context.SubmitChanges( );
}
}
}
[TestMethod]
public void TestUpdateMPN( ) {
using ( var context = new SimoneDataContext( ) ) {
Item item = context.Items.First( );
if ( item != null ) {
item.MPN = "My New MPN";
context.SubmitChanges( );
}
}
}
Unfortunately, TestUpdateName() fails with the following error:
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'..
And here’s the outputted SQL:
UPDATE [dbo].[Items] SET WHERE ([Id]
= @p0) AND ([CategoryId] = @p1) AND ([MPN] = @p2) AND ([Height] = @p3) AND
([Width] = @p4) AND ([Weight] = @p5)
AND ([Length] = @p6) AND
([AdministrativeCost] = @p7)
— @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
— @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
— @p2: Input VarChar (Size = 10; Prec = 0; Scale = 0) [My New MPN]
— @p3: Input Decimal (Size = 0; Prec = 5; Scale = 3) [30.000]
— @p4: Input Decimal (Size = 0; Prec = 5; Scale = 3) [10.000]
— @p5: Input Decimal (Size = 0; Prec = 5; Scale = 3) [40.000]
— @p6: Input Decimal (Size = 0; Prec = 5; Scale = 3) [30.000]
— @p7: Input Money (Size = 0; Prec = 19; Scale = 4) [350.0000]
— Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build:
3.5.30729.4926
As you can see, no update is being generated (SET is empty …)
I have no clue why is this happening.
And already in advance … YES, the table Item has a PK (Id).
Thank you in advance!
Update:
It appears that the error is caused by overriding GetHashcode().
This is my current implementation:
return string.Format( "{0}|{1}|{2}|{3}", Name, Id, UPC, AdministrativeCost).GetHashCode( );
It sounds like your DBML might be out of synch. You should delete the tables and re-add them and try and run it again.
Just delete your
Itemstable manually and re-add it.EDIT: Based on your edit, you should check out the following thread regarding
GetHashCode.http://social.msdn.microsoft.com/forums/en-US/linqtosql/thread/6cc6c226-f718-4b22-baad-dba709afe74b/