当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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??

网友答案:

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();
分享给朋友:
您可能感兴趣的文章:
随机阅读: