2
votes

I've got this question that's been bugging me and my vast and unfathomable intellect just can't grasp it. The case: I want to make multiple one-to-many relationships to the same entity using the fluent nhibernate automapper and export schema.

I have:

Base Class:

 public abstract class Post<T> : Entity, IVotable, IHierarchy<T>
 {
    public virtual string Name
    {
        get; set;
    }

    public virtual string BodyText
    {
        get; set;
    }

    public virtual Member Author
    {
        get; set;
    }
}

and Inheriting Class:

 [Serializable]
public class WallPost : Post<WallPost>
{
    public virtual Member Receiver
    {
        get; set;
    }
}

The 'Member' properties of WallPost is a foreign key relationship to this class:

public class Member : Entity
    {
        public Member()
        {
           WallPosts = new IList<WallPost>();
        }

 public virtual IList<WallPost> WallPosts
        {
            get; set;
        }
}

I hope you're with me until now. When I run the exportschema of nhibernate I expect to get a table wallpost with 'author_id' and 'receiver_id' BUT I get author_id, receiver_id,member_id. Why did the Nhibernate framework add member_id, if it's for the collection of posts (IList) then how do you specify that the foregin key relationship it should use to populate is receiver, i.e. member.WallPosts will return all the wallposts of the receiver.

I hope i made sense, if you need anything else to answer the question let me know and I'll try to provide.

Thanks in advance

P.s. If I change the property name from 'Receiver' to 'Member' i.e. public virtual Member Member, only two associations are made instead of 3, author_id and member_id.

E

THE SOLUTION TO THIS QUESTION FOR ANYONE ELSE WONDERING IS TO ADD AN OVERRIDE:

mapping.HasMany(x => x.WallPosts).KeyColumn("Receiver_id");
1

1 Answers

3
votes

Most likely (I don't use auto mapping, I perfer to write my own classmaps) it's because the auto mapping assuming that your Member's "WallPosts" is controled by the wall posts. So it creates a member_id for that relationship.

Edit: Try adding an override in your fluent config, after AutoMap add:

.Override<Member>(map =>
{
  map.HasMany(x => x.WallPosts, "receiver_id")
    .Inverse;
});