我的一个实体有一个EntitySet<>具有[Composition],[Include]和[Association]属性的属性。我在我的域服务中填充此集合,但当我在客户端收到它时检查其内容时,该集合为空。
我正在使用Silverlight 4 RTM以及RIA Services 1.0 RTM。
任何想法我做错了什么?
以下是我服务方面的代码:
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;
}
}
答案 0 :(得分:0)
确定。问题是我的域服务在实例化它时没有设置CityDto类的RegionID属性。因此,即使是将城市放入RegionDto的集合中,客户端的RIA服务也无法将这些关联部分组合在一起。
将我的域名服务更改为此可解决此问题。
[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;
}
}