开发者

RIA and POCOs Child Parent Relationship Not Returning All Data

I am trying to setup a POCO RIA Silverlight project. When the domain service returns the data is is missing data.

Below I have included the Parent / Child classes and my Domain Service. It should return a list of 5 Parent Objects with each parent object containing 3 Child objects. I believe I have setup the domain service to correctly return the object tree I desire.

The domain service returns 5 Parent Objects. The first parent object correctly contains 3 child objects.

Howeve开发者_开发百科r all subsequent parent object do not contain child objects. I am doing something wrong.

I have found a few resources to help and I seem to be following their prescribed methods with no results.

Link 1 Link 2

If anyone can point out what I am doing wrong I would greatly appreciate it.

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

    private List<Child> _children = new List<Child>();

    [Include]
    [Association("ParentChildRelation", "Id", "ParentId")]
    [Composition]
    public List<Child> Children
    {
        get { return _children; }
    }
} 
public class Child
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    private int _parentId;
    public int ParentId { get { return _parentId; } }

    private Parent _parent;        
    [Association("ParentChildRelation", "ParentId", "Id", IsForeignKey = true)]        
    public Parent Parent { get { return _parent; } set { _parent = value; _parentId = value.Id; } }
}
 [EnableClientAccess()]
public class PocoDomainService : DomainService
{
    public List<Parent> GetParents()
    {
        var list = new List<Parent>();
        list.AddRange(from p in Enumerable.Range(1, 5)
                      select (new Parent()
                      {
                          Name = "Parent " + p.ToString(),
                          Id = p
                      }));
        foreach (var p in list)
        {
            p.Children.AddRange(from c in Enumerable.Range(1, 3)
                                select (new Child()
                                {
                                    Id = c,
                                    Name = "Child " + c.ToString() + " From " + p.Name,
                                    Parent = p
                                }));
        }
        return list;
    }
}


The problem was in how I was creating the collection in the domain service.

I was using the same Child ids { 1, 2, 3} for each of the parents children, each child id needed to be unique among all the children. Like it would be in a database. Silly me.


Just an initial comment without looking deeper - you've named the two different associations the same. You may need to make those names unique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜