Detecting whether or not a link has been established to a related entity on the server side RRS feed

  • Question

  • I have an entity StationInventory.  A StationInventory object has an InventoryCutType property.  There is a many:1 foreign key relationship between StationInventory and InventoryCutType.  InventoryCutType must be set for a new StationInventory object.


    I'm using the Silverlight data services client.  If I create a new StationInventory object and don't set InventoryCutType, I get the following exception from the server side when I attempt to save the new object:


    Entities in 'SonixtreamEntities.StationInventory' participate in the 'FX_StationInventory_InventoryCutType' relationship. 0 related 'InventoryCutType' were found. 1 'InventoryCutType' is expected.


    I would like to change that to a friendlier error.  I would like to use a ChangeInterceptor in the DataService entities class on the server-side to throw a validation error in an Exception.  It would look something like the following:


    Code Snippet


    public void OnChangeStationInventory(StationInventory stationInventory, UpdateOperations operation) {

    if (operation == UpdateOperations.Add || operation == UpdateOperations.Change) {

    if (stationInventory.InventoryCutTypeReference == null ||

    stationInventory.InventoryCutTypeReference.Value == null) {

    throw new DataServiceException(400, "Cut Type is required.");







    I can't figure out how to correctly determine whether or not the associated InventoryCutType has been set.  I have some test code that creates a valid StationInventory and calls AddLink to establish the link between StationInventory and InventoryCutType.  If I remove the ChangeInterceptor above and allow the operation to complete, the new StationInventory record is saved to the database correctly, so the save code is definitely working.


    Whether I call AddLink or not, the following statements are always true in the ChangeInterceptor above:

    • stationInventory.InventoryCutType is null
    • stationInventory.InventoryCutTypeReference is non-null
    • stationInventory.InventoryCutTypeReference.EntityKey is null
    • stationInventory.InventoryCutTypeReference.Value is null

    Since those statements are always true, I haven't found any way to distinguish between the scenario where the link has been correctly set and the scenario where it has not been set (which means I should raise the validation error).


    What's the correct way in Data Services to determine whether the link has been set correctly?




    David Cater

    Thursday, August 14, 2008 8:43 PM

All replies

  • Would it be possible for someone from the Astoria group to take a look at this and provide a suggestion?





    Tuesday, August 19, 2008 5:47 PM