4

我有一个Neo4j 图形数据库,可以通过Neo4jClient访问。(它是 Neo4j 的 REST api 的 .NET 客户端)

有一个文档的开头。

我做了什么

与数据库的连接有效。

Client = new GraphClient(new Uri("http://localhost:7474/db/data"));
Client.Connect();

这样我可以插入节点...

Client.Create(new myNodeClass { name = "Nobody" });

...并查询他们。

Node<myNodeClass> Node = Client.Get<WordNode>(138);
return Node.Data.name;

我想做的事

我只是想添加和更新节点之间的关系。(关系类型必须是数字。)

不幸的是,还没有关于关系的文档。

有一个命令名为CreateRelationship. 但我无法让它工作。

Client.CreateRelationship(Neo4jClient.NodeReference<TSourceNode>, TRelationship);

你能给我一个添加和更新(数字)关系的例子吗?

4

3 回答 3

4

在测试用例中可以找到很多东西......例如:

http://hg.readify.net/neo4jclient/src/4693da483a90/Test/ApiUsageIdeas.cs

于 2012-03-02T17:09:11.603 回答
2

我也被卡住了,然后我意识到我需要在 CreateRelationship 方法中指定源节点引用参数的类型参数。

在此示例中,我创建了关系。我还没有更新关系。

披露(它在我的机器上作为运行 Visual Studio 2012,YMMV 的控制台应用程序工作)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Neo4jClient;

namespace Neo4jClientExample
{

    class MyConsoleProgram
    {

        private GraphClient Client {get;set; }

        static void Main(string[] args)
        {

        try{
            GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"));
            client.Connect();

            Us us = new Us { Name = "We are Us" };
            NodeReference<Us> usRef = client.Create(us);
            Console.WriteLine("us node.id: {0}", usRef.Id);

            var queryUs = client.Cypher.Start("n", "node(" + usRef.Id + ")").Return<Node<Us>>("n");
            Console.WriteLine("Us node name: {0}\n", queryUs.Results.AsEnumerable<Node<Us>>().First().Data);


            AllYourBase allYourBase = new AllYourBase { Name = "We are all your base" };
            NodeReference<AllYourBase> allYourBaseRef = client.Create(allYourBase);
            Console.WriteLine("AllYourBase node.id: {0}",allYourBaseRef.Id);

            var queryAllYourBase = client.Cypher.Start("n", "node(" + allYourBaseRef.Id + ")").Return<Node<AllYourBase>>("n");
            Console.WriteLine("AllYourBase node name: {0}\n", queryAllYourBase.Results.AsEnumerable<Node<AllYourBase>>().First().Data);

            RelationshipReference areBelongToRef = client.CreateRelationship(allYourBaseRef, new AreBelongTo(usRef));

            var query = client.Cypher.Start("allyourbase", "node(" + allYourBaseRef.Id + ")").Match("allyourbase-[:ARE_BELONG_TO]->us").Return<Node<AllYourBase>>("allyourbase");
            query.ExecuteWithoutResults();
            Console.WriteLine("Result of querying for all your base that belongs to us: {0}", query.Results.AsEnumerable<Node<AllYourBase>>().First().Data.Name);
        }
        catch(Exception ex)
        {
            Console.WriteLine("{0}", ex.Message);
            Console.WriteLine("{0}", ex.InnerException);
        }
        Console.ReadKey();
    }
}

public class Us
{
    public string Name {get; set;}

    public Us()
    {
    }
}

public class AllYourBase
{
    public string Name { get; set; }

    public AllYourBase()
    {
    }
}
public class AreBelongTo : Relationship, IRelationshipAllowingSourceNode<AllYourBase>,
                                         IRelationshipAllowingTargetNode<Us>
{
    public AreBelongTo(NodeReference targetNode)
        : base(targetNode)
    {}

    public const string TypeKey = "ARE_BELONG_TO";

    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}
于 2013-04-26T14:54:36.513 回答
1

你可以看看测试,http://hg.readify.net/neo4jclient/src/4693da483a90/Test/RelationshipTests.cs或联系 Neo4j 邮件列表中的作者,groups.google.com/group/neo4j?

于 2012-02-27T11:09:15.333 回答