2012-02-24 66 views
4

我有一個的Neo4j圖數據庫經由Neo4jClient訪問。 (它是Neo4j的REST API的.NET客戶端)Neo4jClient創建和更新關係

有一個documentation的開頭。

我做了什麼

到數據庫工程的連接。

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); 

你能給我一個添加和更新(數字)關係的例子嗎?

回答

2

我插得太深,然後我意識到我需要指定源節點的參考參數的類型參數在CreateRelationship方法中。

在這個例子中,我創建了這種關係。我還沒有更新關係。

披露(它的工作原理在我的機器上作爲控制檯應用程序在運行Visual Studio 2012,因人而異)

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; } 
    } 
}