views:

87

answers:

1

One of my entities has an EntitySet<> property with [Composition], [Include] and [Association] attributes. I populate this collection in my domain service but when I check its contents when it is received on the client, the collection is empty.

I am using Silverlight 4 RTM as well as RIA Services 1.0 RTM.

Any ideas what I am doing wrong?

Here is the code on my service side:

public class RegionDto
{
  public RegionDto()
  {
      Cities = new EntitySet<CityDto>();
  }

  [Key]
  public int Id { get; set; }
  public string Name { get; set; }

  [Include]
  [Composition]
  [Association("RegionDto_CityDto", "Id", "RegionId")]
  public EntitySet<CityDto> Cities { get; set; }
}

public class CityDto
{
  [Key]
  public int Id { get; set; }
  public int RegionId { get; set; }
  public string Name { get; set; }
}

[EnableClientAccess()]
public class RegionDomainService : LinqToEntitiesDomainService<RegionEntities>
{
  public IEnumerable<RegionDto> GetRegions()
  {
      var regions = (ObjectContext.Regions
          .Select(x => new RegionDto
          {
              Id = x.ID,
              Name = x.Name
          })).ToList();

      foreach (var region in regions)
      {
          var cities = (ObjectContext.Cities
              .Where(x => x.RegionID == region.Id)
              .Select(x => new CityDto
              {
                  Id = x.ID,
                  Name = x.Name
              })).ToList();

          foreach (var city in cities)
          {
              region.Cities.Add(city);
          }
      }

      //  each region's Cities collection is populated at this point
      //  however when the client receives it, the Cities collections are all empty
      return regions;
  }
}
A: 

Ok. The problem was the my domain service wasn't setting the RegionID property of the CityDto classes when it instantiated them. So even tho it was putting the cities into the RegionDto's collection, RIA Services on the client couldn't piece the assocation together.

Changing my domain service to this fixed the problem.

[EnableClientAccess()]
public class RegionDomainService : LinqToEntitiesDomainService<RegionEntities>
{
  public IEnumerable<RegionDto> GetRegions()
  {
      var regions = (ObjectContext.Regions
          .Select(x => new RegionDto
          {
              Id = x.ID,
              Name = x.Name
          })).ToList();

      foreach (var region in regions)
      {
          var cities = (ObjectContext.Cities
              .Where(x => x.RegionID == region.Id)
              .Select(x => new CityDto
              {
                  Id = x.ID,
                  Name = x.Name,
                  RegionId = region.Id
              })).ToList();

          foreach (var city in cities)
          {
              region.Cities.Add(city);
          }
      }

      //  each region's Cities collection is populated at this point
      //  however when the client receives it, the Cities collections are all empty
      return regions;
  }
}
Joshua Poehls