【发布时间】:2014-09-11 23:01:53
【问题描述】:
我有一个“Group”类和一个“GroupSummaryLevel”类,代码如下。 DB中的这些实体之间存在一对一的关系。我需要“GroupSummaryLevel”作为 Groups 类中的属性。它应该是一个非常简单的连接,例如
(从 GroupSummaryLevel g WHERE g.AcctGroup = GroupID 中选择 g.Id)
不幸的是,我无法弄清楚如何使用 NHibernate。我在这里看到的许多答案对我没有帮助。我会从那里更有经验的 NHibernate 用户那里获得任何意见。提前致谢。
public class Group : DomainEntity
{
public virtual string GroupId { get; set; }
public virtual string GroupName { get; set; }
public virtual GroupSummaryLevel GroupSummaryLevel { get; set; }
}
public class GroupSummaryLevel : DomainEntity
{
public virtual int Id { get; set; }
public virtual string AcctGroup { get; set; }
public virtual GroupSummaryLevel Parent { get; set; }
public virtual IList<GroupSummaryLevel> Children { get; set; }
public GroupSummaryLevel()
{
Children = new List<GroupSummaryLevel>();
}
}
到目前为止,我所做的映射不起作用。我的映射代码如下:
public GroupMap()
{
Table("Groups");
LazyLoad();
Id(x => x.GroupId).GeneratedBy.Assigned().Column("GroupID").CustomType<TrimmedString>();
Map(x => x.GroupName).Column("GroupName").CustomType<TrimmedString>().Not.Nullable();
HasOne(x => x.GroupSummaryLevel).Cascade.None().ForeignKey("AcctGroup");
}
public GroupSummaryLevelMap()
{
Table("GroupSummaryLevel");
LazyLoad();
Id(x => x.Id).GeneratedBy.Identity().Column("Id");
Map(x => x.AcctGroup).Column("AcctGroup").CustomType<TrimmedString>().Not.Nullable();
//References(x => x.Parent).Column("ParentId");
//HasMany(x => x.Children).Cascade.All().KeyColumn("ParentId");
}
注意:我还需要为 GroupSummaryLevel 进行自加入,但也没有成功。对此的任何建议也将不胜感激:)
【问题讨论】:
标签: c# nhibernate fluent-nhibernate