nhibernate - 流利的Nhibernate: self 参照

标签 nhibernate fluent-nhibernate

使用此类和映射:

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: self 参照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285424/

相关文章:

c# - 如何使用 NHibernate 和 Envers 交换唯一的、排序的列条目?

c# - 流利的 nHibernate : Use the same mapping files for tables with the same structure in different schemas

c# - 如何获取 NHibernate 中的连接标准计数

c# - Fluent NHibernate 两级继承问题

asp.net - Web API ASP.NET 中的 NHibernate : No session bound to the current context

c# - 如何将 CaSTLe Activerecord 转换为纯 NHibernate 或 Fluent NHibernate?

c# - 在 NHibernate 中设置超时过期

c# - 将 Spring.NET 与 NHibernate 3 一起使用

nhibernate - 枚举到整数的映射导致每次刷新时都会更新

c# - 多列多对一