Search code examples
c#entity-frameworkwcf-ria-services

Can't update Composite data with wcf ria servics


Suppose I have entity MyTab which has composite property MyList. WCF generates code fors MyTab for update like:

public void UpdateMyTab(MyTab currentMyTab) 
{          
    this.ObjectContext.MyTabs
                      .AttachAsModified(currentMyTab, 
                                        this.ChangeSet.GetOriginal(currentMyTab));
}

When only composite data changed, MyTab data not changed, then submit changes, I will get error "Value cannot be null.\r\nParameter name: original." because no orginal for MyTab. changeset will have 2 item: MyTab and MyList.

How to resolve this problem?


Solution

  • You should check first the return value of GetOriginal if its null, before callling AttachAsModified. If GetOriginal returns null you should Attach the entity only to the ObjectContext. I don´t tested it but i would update your method to the following.

    public void UpdateMyTab(MyTab currentMyTab) {          
      var original = this.ChangeSet.GetOriginal(currentMyTab);
    
      if (original != null) {
        this.ObjectContext.MyTabs.AttachAsModified(currentMyTab, original);
      }
      else {
        this.ObjectContext.MyTabs.Attach(currentMyTab);
      }
    }