简体   繁体   中英

Mapping a many-to-two relationship in fluent-nhibernate

I'm working with a node/link structure, but I'm having problems mapping it using fluent nhibernate.

This is a simplification of the classes I'm using.


class Node
{
  public virtual IList Links { get; set; }
}

class Link
{
  public virtual Node StartNode { get; set; }
  public virtual Node EndNode { get; set; }
}

A node can have many links connected to it. A link has to be connected to two nodes.

And I need to know which node is the start node and end node, so they have to be specific. Which is why I can not use a list and limit it to two nodes.

Has anyone come across this problem and found a solution to it?

Edit: Clearifying question
I'm not using Automapping, I'm using the explisit mapping methods: References, HasMany and HasManyToMany. Essentially following the methods found in the introductory tutorial: http://wiki.fluentnhibernate.org/Getting_started#Your_first_project

I don't have a database either, I'll create the database schema from the mappings using nhibernate.

What I'm asking is, how do I create a many-to-two relation?

Well there's not a special many to two relationship but what you'd probably do is something like this:

public class NodeMap : ClassMap<Node>
{
    public NodeMap()
    {
        //Id and any other fields mapped in node

        HasMany(x => x.Links);
    }
}

public class LinkMap : ClassMap<Link>
{
    public LinkMap()
    {
        //Id and any other fields mapped in node

        References(x => x.StartNode);
        References(x => x.EndNode);
    }
}

Again this is just a brief overview above. You will probably need additional mapping attributes if you want to for example cascade any create/update/delete actions.

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