开发者

Self referencing many to many relationships in Fluent NHibernate automapping

开发者 https://www.devze.com 2023-02-15 20:41 出处:网络
The title pretty much explains it all, I have a Member object that references \'Friends\' who are also type Member.

The title pretty much explains it all, I have a Member object that references 'Friends' who are also type Member.

 public class Member : Entity
    {
        public Member()
        {            
            Friends = new List<Member>();
        }

        public virtual IList<Member> Friends
        {
            get; set;
        }
     }

The schema generation tool makes it a 1:n relationship while it should be a n:n relationship i.e. a column is added to the member table called member_id and no connecti开发者_运维百科ng table is created.

Is there any way to make a Self referencing many to many relationships in Fluent NHibernate?


Definitely possible. You just need to create an override

public class MemberOverride : IAutoMappingOverride<Member>
{
    public void Override(AutoMapping<Member> mapping)
    {
        mapping.HasManyToMany(m => m.Friends)
               .Table("MemberFriendsLinkTable");
               .ParentKeyColumn("MemberId")
               .ChildKeyColumn("FriendId");
    }
}

Just tell the auto mapping configuration where your overrides are to have these included.

EDIT: Updated to include Parent and Child key columns

0

精彩评论

暂无评论...
验证码 换一张
取 消