开发者

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_开发者_如何学Cproject

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜