0

I have a table called "orderDetails" which contains 4 fields:

OrderID // (primary key of Orders table)
ItemID // (primary key of Items table)
Amount
IsImportant

the primary key of orderDetails table is composed by the first two fields.

I have in my asp.net site a gridview which shows the order details of a selected order.

I'm trying to update a row in the gridview. the user can update only the Amount, IsImportant fileds. For all the rows except the first one i'm getting this error when trying to update a row:

The property 'ItemID' is part of the object's key information and cannot be modified.

I read that it is not possible to update the primary key but this is weird because

  1. I'm not trying to update the primary key, only the rest of the fields
  2. updating the first row in the gridview does succeed.

Thanks!

Community
  • 1
  • 1
adl
  • 1,865
  • 1
  • 25
  • 32
  • Is it a sqlException or EF exception? If this is sent from DB please check what is passed a s a query via Profiler tool – Dave May 31 '12 at 12:47
  • if it helps - the error is thrown at the line `ReportPropertyChanging("ItemID");` in the setter of the ItemID Property. this code is autogenerated in the file `model.designer.cs`. the model is my EF name. – adl May 31 '12 at 12:52
  • Is the StoreGeneratedPatern of this property set to Identity? – Dave May 31 '12 at 12:58
  • try to have a look http://stackoverflow.com/questions/5466374/composite-key-with-ef-4-1-code-first – Dave May 31 '12 at 13:12
  • If you are getting that exception then something is trying to write a value to the ItemID property. From the descrioption my guess is that something is trying to write zero into that property. However, without any code it's very hard to know what is doing this. – Arthur Vickers May 31 '12 at 15:38

1 Answers1

0

I'm not sure this is what you're looking for, but try to set the column of the problematic key to visible = false. Because when you do update it basically tries to updates ALL fields which appears on that row.

But it's still weird why the first row's update did succeed.

BornToCode
  • 9,495
  • 9
  • 66
  • 83