简体   繁体   中英

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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