Redplane
Redplane

Reputation: 3151

Neo4j with C# : Create relation between 2 existing nodes, otherwise dont

I'm making an application which is used for managing chat.

I have 2 C# classes :

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

    public string Password { get; set; }
}

public class PrivateMessage
{
    public long Id { get; set; }

    public string Content { get; set; }

    public long Stamp { get; set; }

    public bool Received { get; set; }
}

All I want is to create a relation with PrivateMessage's properties between 2 people.

I created a cypher query like this :

// Create a message from 'Doctor B' to 'Patient B'.
MATCH (Sender:Person), (Recipient:Person)
WHERE Sender.Name = 'Doctor B' AND Recipient.Name = 'Patient B'
CREATE (Sender)-[Message:SENT {Id: 0, Content: 'Hello', Stamp: 1000, Received: false}]->(Recipient)

But I don't know how to archive with using C#. Also, I want the relation can only be created as 2 nodes exist, otherwise, it can't.

You can watch my image for more detail:

enter image description here

As you can see, I have 2 entities : Doctor B and Patient B, I can create a SENT relationship between them.

But if I use this query:

MATCH (Sender:Person), (Recipient:Person) WHERE Sender.Name = 'Doctor A' AND Recipient.Name = 'Patient A' CREATE (Sender)-[Message:SENT {Id: 0, Content: 'Hello', Stamp: 1000, Received: false}]->(Recipient)

Because there is no Doctor A and Patient A, neo4j create 2 grey nodes with a SENT relationship between them.

My question is : How I can prevent this, I don't want to create grey nodes in this image.

Can anyone help me please ? Thank you.

Upvotes: 0

Views: 1459

Answers (1)

Charlotte Skardon
Charlotte Skardon

Reputation: 6270

A relationship would only ever be created if the two nodes exist, otherwise it'd be a hanging line :)

C# wise - you're looking at:

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

//The message to be sent.    
var message = new Message {Id = 0, Content = "Hello", Stamp = 1000, Recieved = false};

var query = client.Cypher
    .Match("(sender:Person)", "(recipient:Person)")
    //These 'Wheres' create parameters in the query
    .Where((Person sender) => sender.Name == "Person A")
    .AndWhere((Person recipient) => recipient.Name == "Person B")
    .Create("(sender)-[msg:SENT {message}]->(recipient)")
    //The message is added as a parameter here
    .WithParam("message", message)
    .Return(msg => msg.As<Message>());

var msg = query.Results.Single();

I'm not sure why you're returning the message, if you decide not to, just change the code to:

var query = client.Cypher
    .Match("(sender:Person)", "(recipient:Person)")
    .Where((Person sender) => sender.Name == "Person A")
    .AndWhere((Person recipient) => recipient.Name == "Person B")
    .Create("(sender)-[msg:SENT {message}]->(recipient)")
    .WithParam("message", message);

query.ExecuteWithoutResults();

Either way, the relationship will only be created if both sender and recipient exist.

Upvotes: 1

Related Questions