使用此类和映射:

Public class Something
{
    public int Id;
    public IList<Something> Similarthings { get; set; }
}

public class SomtehingMapping
    {
        public SomtehingMapping()
        {
            Map(x => x.Id);
            HasManyToMany(x => x.Similarthings )
               .Table("SomethingsToSimilarthings")
               .ParentKeyColumn("SomethingA_Id")
               .ChildKeyColumn("SomethingB_Id")
               .Cascade.All();
         }
}


您最终得到以下结果:

Table SomethingsToSimilarthings
-------------------------------
SomethingA_Id    SomethingB_Id
111              222
222              111


是否有任何定义此映射的方法,以便仅使用一个数据库行来表示双向关系?

最佳答案

您是否尝试过将逆映射属性设置为true?

public class SomtehingMapping
    {
        public SomtehingMapping()
        {
            Map(x => x.Id);
            HasManyToMany(x => x.Similarthings )
               .Inverse()
               .Table("SomethingsToSimilarthings")
               .ParentKeyColumn("SomethingA_Id")
               .ChildKeyColumn("SomethingB_Id")
               .Cascade.All();
         }
}


一种替代方法是显式定义关联的另一面,并将其标记为inverse="true"

关于nhibernate - 流利的Nhibernate:自我参照,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4285424/

10-13 01:02