我使用Neo4j数据库处理C#.net项目,无法使用C#.Net在Neo4j中创建现有节点与新节点之间的关系。
我正在使用以下代码创建节点
// Create entities
var refA = client.Create(new Person() { FName = "Person A" });
var refB = client.Create(new Person() { FName = "Person B" });
var refC = client.Create(new Person() { FName = "Person C" });
var refD = client.Create(new Person() { FName = "Person D" });
// Create relationships
client.CreateRelationship(refA, new KnowsRelationship(refB));
client.CreateRelationship(refB, new KnowsRelationship(refC));
client.CreateRelationship(refB, new HatesRelationship(refD, new HatesData("Crazy guy")));
client.CreateRelationship(refC, new HatesRelationship(refD, new HatesData("Don't know why...")));
现在我想创建一个使用某些条件从数据库获取的现有节点与创建新节点之间的关系。但不能这样做
答案 0 :(得分:1)
如果您希望使用C#中的Neo4J,您仍然需要对Cypher有充分的了解。
您可以在此处找到相关文档:
Cypher query language - documentation
我创建了一个示例程序,用于在它们之间创建3个节点和2个关系,然后查询与节点1相关的节点。
using Neo4jClient;
using Neo4jClient.Cypher;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Neo4JTest
{
class Program
{
static void Main(string[] args)
{
try
{
GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
// Create nodes and relationship
MyNode node1 = new MyNode() { Name = "Test 1" };
MyNode node2 = new MyNode() { Name = "Test 2" };
NodeReference<MyNode> node1ref = client.Create<MyNode>(node1);
NodeReference<MyNode> node2ref = client.Create<MyNode>(node2);
MyRelationShip rel12 = new MyRelationShip(node2ref);
var Rel1 = client.CreateRelationship<MyNode, MyRelationShip>(node1ref, rel12);
MyNode node3 = new MyNode() { Name = "Test 3" };
NodeReference<MyNode> node3ref = client.Create<MyNode>(node3);
MyRelationShip rel13 = new MyRelationShip(node3ref);
var Rel13 = client.CreateRelationship<MyNode, MyRelationShip>(node1ref, rel13);
var query = client.Cypher.Start(new { n1 = node1ref })
.Match("n1-[:MYRELATIONSHIP]->targetnode")
.Return<MyNode>(targetnode => targetnode.As<MyNode>());
var res = query.Results;
int i = 0;
foreach (MyNode n in res)
{
i++;
Console.WriteLine(i + ". Name: '" + n.Name + "'");
}
}
catch(NeoException ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
class MyNode
{
private string _name = "";
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
public class MyRelationShip : Relationship, IRelationshipAllowingSourceNode<MyNode>, IRelationshipAllowingTargetNode<MyNode>
{
public static readonly string TypeKey = "MYRELATIONSHIP";
public MyRelationShip(NodeReference targetNode)
: base(targetNode)
{ }
public override string RelationshipTypeKey
{
get { return TypeKey; }
}
}
}
}