Linq to SQL - Failed to update

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( );

+7
c # sql-server linq-to-sql
source share
2 answers

It looks like your DBML may not be in sync. You have to delete the tables and add them again and try and run them again.

Just delete the Items table manually and re-add it.

EDIT . Based on your edit, you should check out the following topic regarding GetHashCode .

http://social.msdn.microsoft.com/forums/en-US/linqtosql/thread/6cc6c226-f718-4b22-baad-dba709afe74b/

. Pure rules state that GetHashCode () and Equals () should always be implemented in tandem. Two equal objects must have the same hash code.

In addition, the combination of GetHashCode () + Equals () forms the concept of the essence of personality. If you do this based on the field value (except PK), then the identity changes when the field changes. This is bad if L2S needs to look for other information in the dictionary based on the identity of the entity, and especially if L2S needs to find the object in its cache identifier!

Tip: do not change the identity of the entity. L2S expects it to be based on a natural object (address based).

+12
source share

It seems that the generated SQL does not include the contents of the SET clause (note that after SET there is no [Name] = @pXX ). All properties (data type, size, etc.) Are the object fields set correctly in the dbml designer?

0
source share

All Articles