Collection is empty when it arrives on the client

Posted by digiduck on Stack Overflow See other posts from Stack Overflow or by digiduck
Published on 2010-05-24T16:27:00Z Indexed on 2010/05/24 16:31 UTC
Read the original article Hit count: 449

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;
  }
}

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-4.0