wcf ria服务 - 与父母子女关系的poco

时间:2011-07-12 18:54:40

标签: silverlight wcf-ria-services

我有两个域类:Parent和Child。 父母有一个Child列表。当我使用域服务来检索父对象列表时,我想检索每个父对象的子对象。这里是域类,域服务和客户端代码:

 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 code:只是一个带有事件处理程序的按钮:

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

1 个答案:

答案 0 :(得分: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; }
}

如果您应用这些更改,则应尊重实体的层次结构,并将子项包含在加载操作中。