实体类
1 public class Dept : IEntity 2 { 3 public virtual long Id { get; set; } 4 5 public Dept() 6 { 7 ChildDepts = new HashedSet<Dept>(); 8 } 9 10 /// <summary> 11 /// 部门名称 12 /// </summary> 13 public virtual string Name { get; set; } 14 /// <summary> 15 /// 部门描述 16 /// </summary> 17 [Length(Max = 1024, Message = "名字长度超过1024位")] 18 public virtual string Remark { get; set; } 19 /// <summary> 20 /// 上级部门 21 /// </summary> 22 public virtual Dept ParentDept { get; set; } 23 /// <summary> 24 /// 下级部门 25 /// </summary> 26 public virtual ISet<Dept> ChildDepts { get; set; } 27 28 public virtual void AddChildDept(Dept dept) 29 { 30 ChildDepts.Add(dept); 31 dept.ParentDept = this; 32 } 33 }
mapping配置
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using FluentNHibernate.Mapping; 6 using MOGO.OA.Domain; 7 namespace MOGO.OA.ORM.Mapping 8 { 9 public class DeptMap : ClassMap<Dept> 10 { 11 public DeptMap() 12 { 13 Table("Dept"); 14 Id(p => p.Id).GeneratedBy.Identity(); 15 Map(p => p.Name).Not.Nullable().Length(64); 16 Map(p => p.Remark).Nullable().Length(1024); 17 HasOne<Dept>(p => p.ParentDept); 18 HasMany<Dept>(p => p.ChildDepts).LazyLoad().Cascade.All().KeyColumn("ParentDept"); 19 } 20 } 21 }
现在的问题是,每次ParentDept读取出来是本部门,而不是上层的部门,这个树形结构的mapping应该如何配置