[英]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. 我正在使用节点/链接结构,但是使用流畅的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. 我不使用自动映射,而是使用显式映射方法:References,HasMany和HasManyToMany。 Essentially following the methods found in the introductory tutorial: http://wiki.fluentnhibernate.org/Getting_started#Your_first_project
基本上遵循入门教程中找到的方法: 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. 我也没有数据库,我将使用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.
例如,如果要级联任何创建/更新/删除操作,则可能需要其他映射属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.