【问题标题】:How do I map to a parent in the same table with NHiberNate using C# (ClassMapping)如何使用 C# (ClassMapping) 使用 NHiberNate 映射到同一个表中的父级
【发布时间】:2013-05-05 15:49:12
【问题描述】:

我有一个下面的类,它有一个相同类型的父类,

public class Unit
{
    public virtual int UnitKey { get; set; }
    public virtual string UnitName { get; set; }
    public virtual string UnitType { get; set; }
    public virtual Unit Parent { get; set; }
    public virtual User UnitLeader { get; set; }
}

在数据库中有一个名为 Unit 的表

Unit_Key (int)
Unit_Name (varchar)
Unit_Type (varchar)
Parent_Key (varchar)
User_Key (int)

这是映射类的方法,

internal class UnitMapping : ClassMapping<Unit>
{
    public UnitMapping()
    {
        Id(x => x.UnitKey, map => 
        { 
            map.Column("Unit_Key"); 
            map.Generator(Generators.Assigned); 
        });
        Property(x => x.UnitName, map => 
        { 
            map.Column("Unit_Name"); 
            map.NotNullable(true); 
        });
        Property(x => x.UnitType, map =>
        {
            map.Column("Unit_Type"); map.NotNullable(true);
        });

        ManyToOne(x => x.UnitLeader, map => 
        { 
            map.Column("User_Key"); map.Cascade(Cascade.None); 
        });
    }
}

如何映射同一张表中的 Parent(同一类的类型)。

谢谢

【问题讨论】:

    标签: c# .net nhibernate mapping nhibernate-mapping


    【解决方案1】:

    与处理不同实体的方式完全相同。

    您已经在映射ManyToOne (UnitLeader); Parent 也不例外。

    【讨论】:

    • 是否使用此映射进行级联删除?
    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多