views:

312

answers:

1

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

A: 

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();
Dve

related questions