NHibernate字节码中的分层实体父键

NHibernate字节码中的分层实体父键,nhibernate,Nhibernate,使用NHibernate 3.2字节码配置,我试图映射以下层次实体: public class BusinessType { public virtual Guid BusinessTypeId { get; set; } public virtual Guid? ParentBusinessTypeId { get; set; } public virtual String BusinessTypeName { get; set; } public virtua

使用NHibernate 3.2字节码配置,我试图映射以下层次实体:

public class BusinessType
{
    public virtual Guid BusinessTypeId { get; set; }
    public virtual Guid? ParentBusinessTypeId { get; set; }
    public virtual String BusinessTypeName { get; set; }
    public virtual ICollection<BusinessType> Children { get; set; }
}
公共类业务类型
{
公共虚拟Guid BusinessTypeId{get;set;}
公共虚拟Guid?ParentBusinessTypeId{get;set;}
公共虚拟字符串BusinessTypeName{get;set;}
公共虚拟ICollection子项{get;set;}
}
使用此类映射:

public class BusinessTypeMapper : ClassMapping<BusinessType>
{
    public BusinessTypeMapper()
    {
        Id(x => x.BusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.BusinessTypeName);
        Set(x => x.Children,
            cm =>
                {
                    // This works, but there is an ugly string in here
                    cm.Key(y => y.Column("ParentBusinessTypeId"));
                    cm.Inverse(true);
                    cm.OrderBy(bt => bt.BusinessTypeName);
                    cm.Lazy(CollectionLazy.NoLazy);
                },
            m => m.OneToMany());
    }
}
公共类BusinessTypeMapper:类映射
{
public BusinessTypeMapper()
{
Id(x=>x.BusinessTypeId,x=>x.Type(新的GuidType());
属性(x=>x.ParentBusinessTypeId,x=>x.Type(新的GuidType());
属性(x=>x.BusinessTypeName);
集合(x=>x.子对象,
厘米=>
{
//这是可行的,但这里有一条难看的线
cm.Key(y=>y.Column(“ParentBusinessTypeId”);
cm.逆(真);
cm.OrderBy(bt=>bt.BusinessTypeName);
cm.Lazy(CollectionLazy.NoLazy);
},
m=>m.OneToMany());
}
}
这很好,但我更希望能够使用lambda指定关系的键,以便重构工作。这似乎是可用的,如下所示:

public class BusinessTypeMapper : ClassMapping<BusinessType>
{
    public BusinessTypeMapper()
    {
        Id(x => x.BusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType()));
        Property(x => x.BusinessTypeName);
        Set(x => x.Children,
            cm =>
                {
                    // This compiles and runs, but generates some other column
                    cm.Key(y => y.PropertyRef(bt => bt.ParentBusinessTypeId));
                    cm.Inverse(true);
                    cm.OrderBy(bt => bt.BusinessTypeName);
                    cm.Lazy(CollectionLazy.NoLazy);
                },
            m => m.OneToMany());
    }
}
公共类BusinessTypeMapper:类映射
{
public BusinessTypeMapper()
{
Id(x=>x.BusinessTypeId,x=>x.Type(新的GuidType());
属性(x=>x.ParentBusinessTypeId,x=>x.Type(新的GuidType());
属性(x=>x.BusinessTypeName);
集合(x=>x.子对象,
厘米=>
{
//这将编译并运行,但会生成其他列
cm.Key(y=>y.PropertyRef(bt=>bt.ParentBusinessTypeId));
cm.逆(真);
cm.OrderBy(bt=>bt.BusinessTypeName);
cm.Lazy(CollectionLazy.NoLazy);
},
m=>m.OneToMany());
}
}
问题在于,这会导致NHibernate生成一个名为
businesstype\u key
的列,忽略已配置的
ParentBusinessTypeId
。有没有办法让NHibernate使用lambda来指定关系,而不是字符串

我从不需要从孩子到父母,只需要从父母那里 给孩子们,所以我认为没有必要


然后删除公共虚拟Guid?ParentBusinessTypeId{get;set;}完整。NH将只创建“businesstype_key”(约定),而不创建“ParentBusinessTypeId”。如果您想更改它,那么您必须使用
cm.Key(y=>y.Column(“您的首选columnname”))指定您的首选columnname

有没有理由不映射
公共虚拟业务类型父项{get;set;}
?这能帮我解决问题吗?我从不需要从孩子到父母,只需要从父母到孩子,所以我认为没有必要。好吧,我可以买。不过,这似乎是一个愚蠢的约定,因为它对所有其他列使用属性名,从而导致了一个真正不一致的模式。出于好奇,如果我映射了一个父属性,这是否使我最初想要的结果成为可能?另外,PropertyRef的用途是什么?“如果我确实映射了父属性”->很可能是否定的,我认为约定是相同的。“PropertyRef”->在外键不引用PrimaryKey而引用另一个属性时使用,例如
clientname->client.name
而不是
client\u id->client.id