简体   繁体   中英

Mapping Many-To-Many-Relation on the same table

i am using nhibernate 3.3.1 and fluent-nhibernate 1.3. I try to map the following entity with fluent:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Person> Relations { get; set; }
}

As you can see, my person is in relation with some other persons. So I modeled the DB-Table like the following:

    | Persons
----+----------------------
 PK | Id int
    | Name nvarchar(100)

    | Relations
----+----------------------
 PK | Id int
 FK | Person1Id int
 FK | Person2Id int

So I want a mapping, where the relations of the person is loaded, no matter if the actual person is referenced as Person1Id or Person2Id.

Is this possible with fluent? And if not, is it possible with a hbn.xml mapping file?

Thanks for help, Koray

You can't map it directly . You need to create two properties:

public virtual IList<Person> Relations1 { get; set; }
public virtual IList<Person> Relations2 { get; set; }

And then create a projection property that merges them:

public virtual IEnumerable<Person> Relations
{
    get { return Relations1.Concat(Relations2); }
}

(I'm assuming you know how to do the first part; let me know if you don't)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM