Search code examples
silverlightsilverlight-4.0wcf-ria-services

How to get can CanAddNew to be true for a collection returned by RIA Services


RIA Services is returning a list of Entities that won't allow me to add new items. Here are what I believe to be the pertinent details:

  • I'm using the released versions of Silverlight 4 and RIA Services 1.0 from mid-April of 2010.
  • I have a DomainService with a query method that returns List<ParentObject>.
  • ParentObject includes a property called "Children" that is defined as List<ChildObject>.
  • In the DomainService I have defined CRUD methods for ParentObject with appropriate attributes for the Query, Delete, Insert, and Update functions.
  • The ParentObject class has an Id property marked with the [Key] attribute. It also has the "Children" property marked with the attributes [Include], [Composition], and [Association("Parent_Child", "Id", "ParentId")].
  • The ChildObject class has an Id marked with the [Key] attribute as well as a foreign key, "ParentId", that contains the Id of the parent.

On the client side, data is successfully returned and I assign the results of the query to a PagedCollectionView like this:

_pagedCollectionView = new PagedCollectionView(loadOperation.Entities);  

When I try to add a new ParentObject to the PagedCollectionView like this:

ParentObject newParentObject = (ParentObject)_pagedCollectionView.AddNew();  

I get the following error:

" 'Add New' is not allowed for this view."

On further investigation, I found that _pagedCollectionView.CanAddNew is "false" and cannot be changed because the property is read-only.

I need to be able to add and edit ParentObjects (with their related children, of course) to the PagedCollectionView. What do I need to do?


Solution

  • For my particular situation, I believe the best fit is this (Your Mileage May Vary):

    Use a PagedCollectionView (PCV) as a wrapper around the context.EntityNamePlural (in my case, context.ParentObjects) which is an EntitySet. (Using loadOperation.Entities doesn't work for me because it is always read-only.)

    _pagedCollectionView = new PagedCollectionView(context.ParentObjects);
    

    Then bind to the PCV, but perform add/delete directly against the context.EntityNamePlural EntitySet. The PCV automatically syncs to the changes done to the underlying EntitySet so this approach means I don't need to worry about sync issues.

    context.ParentObjects.Add();
    

    (The reason for performing add/delete directly against the EntitySet instead of using the PCV is that PCV's implementation of IEditableCollectionView is incompatible with EntitySet causing IEditableCollectionView.CanAddNew to be "false" even though the underlying EntitySet supports this function.)

    I think Kyle McClellan's approach (see his answer) may be preferred by some because it encapsulates the changes to the EntitySet, but I found that for my purposes it was unneccessary to add the ObservableCollection wrapper around loadOperation.Entities.

    Many thanks to to Dallas Kinzel for his tips along the way!