Neo4JClient:如何建立关系?

时间:2012-06-09 13:22:11

标签: .net neo4j

我看到一个标题相同的帖子,但不幸的是,通过查看给定的例子找不到一个问题

我按照以下方式创建了节点:

private static void createExampleNetwork(GraphClient client)
    {
        try
        {
            //CypherQuery cq = new CypherQuery();
            //string createNodeQuery = "CREATE (a {name : 'Andres'}) RETURN";
            //string response = cq.CreateRequest(createNodeQuery);

            var myNodeReferenceA = client.Create(new ExampleNode { Name = "A" });
            var myNodeReferenceB = client.Create(new ExampleNode { Name = "B" });
            var myNodeReferenceC = client.Create(new ExampleNode { Name = "C" });
            var myNodeReferenceD = client.Create(new ExampleNode { Name = "D" });
            var myNodeReferenceE = client.Create(new ExampleNode { Name = "E" });
            var myNodeReferenceF = client.Create(new ExampleNode { Name = "F" });
            var myNodeReferenceG = client.Create(new ExampleNode { Name = "G" });
        }
        catch (Exception ex)
        {

        }
    }

我想创建以下关系: 一个 - B A - Ç 广告 光盘 乙 - G ^

但不知道如何。

我看了一些例子 http://hg.readify.net/neo4jclient/src/4693da483a90/Test/RelationshipTests.cshttp://hg.readify.net/neo4jclient/src/4693da483a90/Test/ApiUsageIdeas.cs

但仍然不知道如何创建关系。

如果有人能提供一个有用的例子,我将非常感激。

1 个答案:

答案 0 :(得分:4)

我找到了解决方案

private static void createExampleNetwork(GraphClient client)
    {
        try
        {

            var myNodeReferenceA = client.Create(new ExampleNode { Name = "A" });
            var myNodeReferenceB = client.Create(new ExampleNode { Name = "B" });
            var myNodeReferenceC = client.Create(new ExampleNode { Name = "C" });
            var myNodeReferenceD = client.Create(new ExampleNode { Name = "D" });
            var myNodeReferenceE = client.Create(new ExampleNode { Name = "E" });
            var myNodeReferenceF = client.Create(new ExampleNode { Name = "F" });
            var myNodeReferenceG = client.Create(new ExampleNode { Name = "G" });

            client.CreateRelationship(myNodeReferenceA, new NodeKnows(myNodeReferenceB));
            client.CreateRelationship(myNodeReferenceA, new NodeKnows(myNodeReferenceC));
            client.CreateRelationship(myNodeReferenceA, new NodeKnows(myNodeReferenceD));
            client.CreateRelationship(myNodeReferenceA, new NodeKnows(myNodeReferenceE));
            client.CreateRelationship(myNodeReferenceB, new NodeKnows(myNodeReferenceF));
            client.CreateRelationship(myNodeReferenceB, new NodeKnows(myNodeReferenceG));
            client.CreateRelationship(myNodeReferenceC, new NodeKnows(myNodeReferenceD));
            client.CreateRelationship(myNodeReferenceE, new NodeKnows(myNodeReferenceF));
        }
        catch (Exception ex)
        {

        }
    }

 public class NodeKnows :Relationship, IRelationshipAllowingSourceNode<ExampleNode>, IRelationshipAllowingTargetNode<ExampleNode>
{
    public NodeKnows(NodeReference targetNode)
        : base(targetNode)
    {
    }

    public const string TypeKey = "KNOWS";
    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}