简体   繁体   English

如何使用Neo4jClient在C#.Net中的neo4j现有节点和新节点之间创建关系?

[英]How to create relationship between neo4j existing node and new node in C#.Net using Neo4jClient?

I work on C#.net project with Neo4j database and unable to create relationship between existing node in Neo4j and new node using C#.Net. 我正在使用Neo4j数据库进行C#.net项目,并且无法使用C#.Net在Neo4j中的现有节点和新节点之间创建关系。

I am creating nodes using following code 我正在使用以下代码创建节点

// 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...")));

Now i want to create relationship between one of the existing node getting from database using some condition and creating new node. 现在,我想使用某种条件从数据库中获取现有节点之一与创建新节点之间的关系。 But can't do this 但是不能这样做

If you wnat to be using Neo4J from C# you still need a solid understanding of Cypher. 如果您想使用C#中的Neo4J,您仍然需要对Cypher有深入的了解。

You can find a very documentation about it here: 您可以在这里找到有关它的非常文档:

Cypher query language - documentation 密码查询语言-文档

I have created an example program for you creating 3 nodes and 2 relationships between them, then querying the nodes neigbouring node 1. 我创建了一个示例程序,供您创建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; }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM