Fluent nhibernate mapping problem: many to many self join with additional data

有些话、适合烂在心里 提交于 2019-12-06 08:34:18

问题


I am struggling with mappings for the following sql tables

   |Post              |          |PostRelation     |
   |------------------|          |-----------------|
   |PostId            |1--------*|ParentPostId     |
   |---other stuff--- |1--------*|ChildPostId      |
   |                  |          |RelationType     |

Ideally Id like a property on post called relatedPosts as

 Dictionary <RelationType,IList<Post>>

But at the minute Id just settle for a property on post with an

  IList<PostRelation>.

I successfully used a many to many to get related posts, but this method loses the addtional data.

Any suggestions??


回答1:


I Finally found a solution after much research. So though I would post it in case it can help anyone else in the future. As PostRelation had additional data, it needed to be an entity in its own right.

---PostRelationMap

        Id(x => x.Id, "PostRelationId").GeneratedBy.Identity();

        References(x => x.ParentPost, "ParentPostId")
            .ForeignKey("FK_PostRelation_ParentPost")
            .Fetch.Join()
            .LazyLoad();

        References(x => x.ChildPost, "ChildPostId")
            .ForeignKey("FK_PostRelation_ChildPost")
            .Fetch.Join()
            .LazyLoad();

        Map(x => x.RelationshipType).CustomType<int>().Not.Nullable();

---PostMap

    HasMany(x => x.ChildPosts)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.AllDeleteOrphan()
            .KeyColumn("ChildPostId")
            .LazyLoad();


来源:https://stackoverflow.com/questions/2243251/fluent-nhibernate-mapping-problem-many-to-many-self-join-with-additional-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!