I'm having a problem using WCF Data Services for an entity that has a composite primary key. I'm using the Silverlight 4 Business Application Template and have created an Entity Data Model (EDM) against the database for the relevant tables and created a WCF Data Service for this EDM in the web project. The Silverlight project has a Service Reference to the WCF Data Service. All fairly standard stuff so far :)
For the sake of simplicity and ease of debugging, I've narrowed this down to a very simple scenario as follows:
Lookup
table has columnsId
andData
.Lookup2
table has columnsId
andData
.MyData
table has columnsLookupId
,Lookup2Id
,SomeDate
, andExtraData
.LookupId
is a foreign key to tableLookup
.Lookup2Id
is a foreign key to tableLookup2
.- Columns
LookupId
,Lookup2Id
, andSomeDate
form the primary key for tableMyData
.
开发者_运维问答
I can then load the data from the lookup tables into separate CollectionViewService
s by using separate DataServiceCollection
s. I also load the main data into another CollectionViewSource
via a third DataServiceCollection
. For the ItemsSource
property in the ComboBox
controls, I bind to the relevant CollectionViewSource
, and then bind the SelectedItem
property to the relevant navigation property. This all works fine so far.
I then add a Save Changes button with a Click
handler that calls BeginSaveChanges
. If I change the value of the ExtraData
field, and then click Save Changes the changes are saved without any problems. However, if I change the value of one of the navigation properties I get an exception when BeginSaveChanges
is called:
Error processing request stream. Error encountered in setting value for property 'Lookup'. Please verify that the value is correct.
If I simplify the EDM further so that the foreign key is not part of the composite key, then changes can be saved successfully, however, the composite key in the actual application is an important part of the data model.
Any suggestions on what I'm doing wrong and how to change a navigation property that forms part of a composite key?
Thanks, Derek.
Changing the key property of an entity is not possible in OData. The main reason is that it would change the identity of the entity, but the entire system assumes that identity never changes. From the point of view of OData changing identity is the same as deleting an existing property and adding a new (different) one. It would also mean that the URL of the entity changes, which would not play nice with clients. Without the model I don't know why it fails the way it does for you, but WCF Data Services won't allow you to change the key property directly anyway.
精彩评论