1

假设我有一个 Parent 和 Child 表,以及一个维护它们之间关系的 ParentChild 表。

父类:

public class Parent
{
    public virtual int ParentId { get; set; }
    public virtual bool Deleted { get; set; }

     public Parent()
     {
       _children = new HashedSet<Child>();
     }

     protected ISet<Child> _children;

     public virtual ISet<Child> children
     {
         get { return _children; }
         protected set { _children = value; }
     }
}

儿童班:

public class Child
{
    public virtual int ChildId { get; set; }
    public virtual bool Deleted { get; set; }

     public Child()
     {
       _parents = new HashedSet<Parent>();
     }

     protected ISet<Parent> _parents;

     public virtual ISet<Parent> parents
     {
         get { return _parents; }
         protected set { _parents = value; }
     }
}

和 ParentChild 类:

public class ParentChild 
{
    public virtual int ParentChildId { get; set; }
    public virtual Parent Parent { get; set; }
    public virtual Child Child { get; set; }
    public virtual int DisplayOrderIndex { get; set; }
    public virtual bool Deleted { get; set; }
}

我将如何映射这些,以便我可以对父对象进行更新,该对象还将使用附加数据(不仅仅是父/子 ID)和子表更新联结表。

现在,我正在做:

父地图

public ParentMap()
{
    Table("Parent");
    Id(x => x.ParentId);
    Map(x => x.Deleted);
    HasMany(x => x.ParentChild)
        .Table("Parent_Child")
        .KeyColumn("ParentID")
        .Access.CamelCaseField(Prefix.Underscore)
        .Cascade.AllDeleteOrphan()
        .LazyLoad();
}

儿童地图

public ChildMap()
{
    Table("Child");
    Id(x => x.ChildId);
    Map(x => x.Deleted);
    HasMany(x => x.ParentChild)
        .Table("Parent_Child")
        .KeyColumn("ChildID")
        .Access.CamelCaseField(Prefix.Underscore)
        .Cascade.AllDeleteOrphan()
        .LazyLoad();
}

父子图

public ParentChildMap()
{
    Table("Parent_Child");
    Id(x => x.ParentChildId, "Parent_ChildID");
    References(x => x.Parent, "ParentID");
    References(x => x.Child, "ChildID");
    Map(x => x.DisplayOrderIndex);
    Map(x => x.Deleted);
}

但是当我从 Parent 存储库中提取时,Parent 对象的 ParentChild 集合中的 Child 是 Castle.Proxy 而不是 Child 类型。

应该如何进行这种映射以达到预期的结果?

4

0 回答 0