I am having problems updating linq to sql objects. For some reason, I can update every single field of my item object except the 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 () does not work with the following error: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'..
And here is the SQL output:
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; prefix = 5; scale = 3) [30.000] - @ p4: input decimal (size = 0; prefix = 5; scale = 3) [10.000] - @ p5: enter decimal (size = 0; prefix = 5; scale = 3) [40.000] - @ p6: input decimal (size = 0; prefix = 5; scale = 3) [30.000] - @ p7: Input money (Size = 0; Accuracy = 19; Scale = 4 ) [350.0000] - Context: SqlProvider (Sql2008) Model: Att ributedMetaModel Build: 3.5.30729.4926
As you can see, the update is not created (SET is empty ...) I do not know why this is happening.
And in advance ... YES, the item table has PK (Id). Thank you in advance!
Update: It appears that the error was caused by an override of GetHashcode (). This is my current implementation:
return string.Format( "{0}|{1}|{2}|{3}", Name, Id, UPC, AdministrativeCost).GetHashCode( );
c # sql-server linq-to-sql
Isaac e
source share