2011-07-12 69 views
2

我有兩個域類:父和子。 父母有一個孩子的列表。當我使用域服務檢索父對象的名單,我想檢索每個家長的children.Here是域類,域服務和客戶端代碼:wcf ria服務 - 與父母子女關係的poco

public class Parent 
{ 
    private IList<Child> children; 
    [Key] 
    public virtual int ParentId { get; set; } 
    public Parent() 
    { 
     children = new List<Child>(); 
    } 
    public virtual string Name { get; set; } 
    [Include()] 
    [Association("ParentChild", "ParentId", "ChildId", IsForeignKey = false)] 
    public virtual IList<Child> Children 
    { 
     get { return children; ; } 
     set { children = value; } 
    } 
    public virtual void AddChild(Child child) 
    { 
     child.Parent = this; 
     Children.Add(child); 
    } 
} 
public class Child 
{ 
    [Key] 
    public virtual int ChildId { get; set; } 
    public virtual String Name { get; set; } 
    public virtual String Action { get; set; } 
    [Include] 
    [Association("ParentChild", "ChildId", "ParentId", IsForeignKey = true)] 
    public virtual Parent Parent { get; set; } 
} 

域名服務:

[EnableClientAccess()] 
public class ParentDomainService : DomainService 
{ 
    public IList<Parent> GetParents() 
    { 
     var system = new Parent() { ParentId = 1, Name = "System1" }; 
     system.AddChild(new Child() 
           { 
            Action = "Action1", 
            ChildId = 3, 
            Name = "File1" 
           }); 
     system.AddChild(new Child() 
     { 
      Action = "Action2", 
      ChildId = 5, 
      Name = "File2" 
     }); 
     var result = new List<Parent>() {system}; 
     return result; 

    } 
} 

Silverlight的GUI代碼:只是一個事件處理程序的按鈕:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     var context = new ParentDomainContext(); 
     var entityQuery = context.GetParentsQuery(); 
     context.Load(entityQuery,OnParentsLoaded,null); 
    } 

    private void OnParentsLoaded(LoadOperation<Parent> obj) 
    { 
     var fileSystem = obj.Entities.FirstOrDefault(); 

     if (fileSystem != null) 
     { 
      var memoryFiles = fileSystem.Children.ToList(); 

     } 
    } 

孩子們失蹤了!

請,如果有人可以幫忙,我看不到什麼是缺少的。

在客戶端上生成的代碼:

/// <summary> 
/// The 'Child' entity class. 
/// </summary> 
[DataContract(Namespace="http://schemas.datacontract.org/2004/07/WordsBuilder.DomainModel")] 
public sealed partial class Child : Entity 
{ 

    private string _action; 

    private int _childId; 

    private string _name; 

    private EntityRef<Parent> _parent; 

    #region Extensibility Method Definitions 

    /// <summary> 
    /// This method is invoked from the constructor once initialization is complete and 
    /// can be used for further object setup. 
    /// </summary> 
    partial void OnCreated(); 
    partial void OnActionChanging(string value); 
    partial void OnActionChanged(); 
    partial void OnChildIdChanging(int value); 
    partial void OnChildIdChanged(); 
    partial void OnNameChanging(string value); 
    partial void OnNameChanged(); 

    #endregion 


    /// <summary> 
    /// Initializes a new instance of the <see cref="Child"/> class. 
    /// </summary> 
    public Child() 
    { 
     this.OnCreated(); 
    } 

    /// <summary> 
    /// Gets or sets the 'Action' value. 
    /// </summary> 
    [DataMember()] 
    public string Action 
    { 
     get 
     { 
      return this._action; 
     } 
     set 
     { 
      if ((this._action != value)) 
      { 
       this.OnActionChanging(value); 
       this.RaiseDataMemberChanging("Action"); 
       this.ValidateProperty("Action", value); 
       this._action = value; 
       this.RaiseDataMemberChanged("Action"); 
       this.OnActionChanged(); 
      } 
     } 
    } 

    /// <summary> 
    /// Gets or sets the 'ChildId' value. 
    /// </summary> 
    [DataMember()] 
    [Key()] 
    [RoundtripOriginal()] 
    public int ChildId 
    { 
     get 
     { 
      return this._childId; 
     } 
     set 
     { 
      if ((this._childId != value)) 
      { 
       this.OnChildIdChanging(value); 
       this.RaiseDataMemberChanging("ChildId"); 
       this.ValidateProperty("ChildId", value); 
       this._childId = value; 
       this.RaiseDataMemberChanged("ChildId"); 
       this.OnChildIdChanged(); 
      } 
     } 
    } 

    /// <summary> 
    /// Gets or sets the 'Name' value. 
    /// </summary> 
    [DataMember()] 
    public string Name 
    { 
     get 
     { 
      return this._name; 
     } 
     set 
     { 
      if ((this._name != value)) 
      { 
       this.OnNameChanging(value); 
       this.RaiseDataMemberChanging("Name"); 
       this.ValidateProperty("Name", value); 
       this._name = value; 
       this.RaiseDataMemberChanged("Name"); 
       this.OnNameChanged(); 
      } 
     } 
    } 

    /// <summary> 
    /// Gets or sets the associated <see cref="Parent"/> entity. 
    /// </summary> 
    [Association("ParentChild", "ChildId", "ParentId", IsForeignKey=true)] 
    public Parent Parent 
    { 
     get 
     { 
      if ((this._parent == null)) 
      { 
       this._parent = new EntityRef<Parent>(this, "Parent", this.FilterParent); 
      } 
      return this._parent.Entity; 
     } 
     set 
     { 
      Parent previous = this.Parent; 
      if ((previous != value)) 
      { 
       this.ValidateProperty("Parent", value); 
       if ((previous != null)) 
       { 
        this._parent.Entity = null; 
        previous.Children.Remove(this); 
       } 
       if ((value != null)) 
       { 
        this.ChildId = value.ParentId; 
       } 
       else 
       { 
        this.ChildId = default(int); 
       } 
       this._parent.Entity = value; 
       if ((value != null)) 
       { 
        value.Children.Add(this); 
       } 
       this.RaisePropertyChanged("Parent"); 
      } 
     } 
    } 

    private bool FilterParent(Parent entity) 
    { 
     return (entity.ParentId == this.ChildId); 
    } 

    /// <summary> 
    /// Computes a value from the key fields that uniquely identifies this entity instance. 
    /// </summary> 
    /// <returns>An object instance that uniquely identifies this entity instance.</returns> 
    public override object GetIdentity() 
    { 
     return this._childId; 
    } 
} 

/// <summary> 
/// The 'Parent' entity class. 
/// </summary> 
[DataContract(Namespace="http://schemas.datacontract.org/2004/07/WordsBuilder.DomainModel")] 
public sealed partial class Parent : Entity 
{ 

    private EntityCollection<Child> _children; 

    private string _name; 

    private int _parentId; 

    #region Extensibility Method Definitions 

    /// <summary> 
    /// This method is invoked from the constructor once initialization is complete and 
    /// can be used for further object setup. 
    /// </summary> 
    partial void OnCreated(); 
    partial void OnNameChanging(string value); 
    partial void OnNameChanged(); 
    partial void OnParentIdChanging(int value); 
    partial void OnParentIdChanged(); 

    #endregion 


    /// <summary> 
    /// Initializes a new instance of the <see cref="Parent"/> class. 
    /// </summary> 
    public Parent() 
    { 
     this.OnCreated(); 
    } 

    /// <summary> 
    /// Gets the collection of associated <see cref="Child"/> entity instances. 
    /// </summary> 
    [Association("ParentChild", "ParentId", "ChildId")] 
    public EntityCollection<Child> Children 
    { 
     get 
     { 
      if ((this._children == null)) 
      { 
       this._children = new EntityCollection<Child>(this, "Children", this.FilterChildren, this.AttachChildren, this.DetachChildren); 
      } 
      return this._children; 
     } 
    } 

    /// <summary> 
    /// Gets or sets the 'Name' value. 
    /// </summary> 
    [DataMember()] 
    public string Name 
    { 
     get 
     { 
      return this._name; 
     } 
     set 
     { 
      if ((this._name != value)) 
      { 
       this.OnNameChanging(value); 
       this.RaiseDataMemberChanging("Name"); 
       this.ValidateProperty("Name", value); 
       this._name = value; 
       this.RaiseDataMemberChanged("Name"); 
       this.OnNameChanged(); 
      } 
     } 
    } 

    /// <summary> 
    /// Gets or sets the 'ParentId' value. 
    /// </summary> 
    [DataMember()] 
    [Editable(false, AllowInitialValue=true)] 
    [Key()] 
    [RoundtripOriginal()] 
    public int ParentId 
    { 
     get 
     { 
      return this._parentId; 
     } 
     set 
     { 
      if ((this._parentId != value)) 
      { 
       this.OnParentIdChanging(value); 
       this.ValidateProperty("ParentId", value); 
       this._parentId = value; 
       this.RaisePropertyChanged("ParentId"); 
       this.OnParentIdChanged(); 
      } 
     } 
    } 

    private void AttachChildren(Child entity) 
    { 
     entity.Parent = this; 
    } 

    private void DetachChildren(Child entity) 
    { 
     entity.Parent = null; 
    } 

    private bool FilterChildren(Child entity) 
    { 
     return (entity.ChildId == this.ParentId); 
    } 

    /// <summary> 
    /// Computes a value from the key fields that uniquely identifies this entity instance. 
    /// </summary> 
    /// <returns>An object instance that uniquely identifies this entity instance.</returns> 
    public override object GetIdentity() 
    { 
     return this._parentId; 
    } 
} 

回答

3

你定義的關聯是不正確的,你需要一些屬性添加到您的域類來解決關聯。

首先,您應該向您的子類中添加一個屬性ParentId以存儲父實體的ID,然後更改父類和子類中的關聯並修復AddChild方法。它應該是這樣的(我改名屬性):

public class Parent { 
    private IList<Child> children; 

    public Parent() { 
    children = new List<Child>(); 
    } 

    [Key] 
    public virtual int Id { get; set; } 

    public virtual string Name { get; set; } 

    [Include()] 
    [Association("Children", "Id", "ParentId")] 
    public virtual IList<Child> Children 
    { 
    get { return children; ; } 
    set { children = value; } 
    } 

    public virtual void AddChild(Child child) 
    { 
    child.Parent = this; 
    child.ParentId = this.Id; 
    Children.Add(child); 
    } 
} 

public class Child { 
    [Key] 
    public virtual int Id { get; set; } 

    public virtual int ParentId {get;set;} 

    public virtual String Name { get; set; } 

    public virtual String Action { get; set; } 

    [Include] 
    [Association("Parent", "ParentId", "Id")] 
    public virtual Parent Parent { get; set; } 
} 

如果你將這些更改您的實體的層次結構應該得到尊重和孩子都包含在你的負荷運行。