5

I'm working with Neo4j using the .Net Neo4jClient (http://hg.readify.net/neo4jclient/wiki/Home). In my code, nodes are airports and relationships are flights.

If I want to create nodes and relationships at the same time, I can do it with the following code:

Classes

public class Airport
{
    public string iata { get; set; }
    public string name { get; set; }
}

public class flys_toRelationship : Relationship, IRelationshipAllowingSourceNode<Airport>, IRelationshipAllowingTargetNode<Airport>
{
    public static readonly string TypeKey = "flys_to";

    // Assign Flight Properties
    public string flightNumber { get; set; }

    public flys_toRelationship(NodeReference targetNode)
        : base(targetNode)
    { }

    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}

Main

// Create a New Graph Object
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

// Create New Nodes
var lax = client.Create(new Airport() { iata = "lax", name = "Los Angeles International Airport" });
var jfk = client.Create(new Airport() { iata = "jfk", name = "John F. Kennedy International Airport" });
var sfo = client.Create(new Airport() { iata = "sfo", name = "San Francisco International Airport" });

// Create New Relationships
client.CreateRelationship(lax, new flys_toRelationship(jfk) { flightNumber = "1" });
client.CreateRelationship(lax, new flys_toRelationship(sfo) { flightNumber = "2" });
client.CreateRelationship(sfo, new flys_toRelationship(jfk) { flightNumber = "3" });

The problem, however, is when I want to add relationships to already existing nodes. Say I have a graph consisting of only two nodes (airports), say SNA and EWR, and I would like to add a relationship (flight) from SNA to EWR. I try the following and it fails:

// Create a New Graph Object
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

Node<Airport> departure = client.QueryIndex<Airport>("node_auto_index", IndexFor.Node, "iata:sna").First();
Node<Airport> arrival = client.QueryIndex<Airport>("node_auto_index", IndexFor.Node, "iata:ewr").First();
//Response.Write(departure.Data.iata); <-- this works fine, btw: it prints "sna"

// Create New Relationships
client.CreateRelationship(departure, new flys_toRelationship(arrival) { flightNumber = "4" });

The two errors I'm receiving are as follows:

1) Argument 1: cannot convert from 'Neo4jClient.Node' to 'Neo4jClient.NodeReference'

2) The type arguments for method 'Neo4jClient.GraphClient.CreateRelationship(Neo4jClient.NodeReference, TRelationship)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The method the error is referring to is in the following class: http://hg.readify.net/neo4jclient/src/2c5446c17a65d6e5accd420a2dff0089799cbe16/Neo4jClient/GraphClient.cs?at=default

Any ideas?

Brent Barbata
  • 3,631
  • 3
  • 24
  • 23
  • Hi Brent, could you please explain how you added the indexes onto the node? I created the index but I think I am missing something, my ``client.QueryIndex`` always returns null.. May be you could answer my question here? http://stackoverflow.com/questions/14358797 Also, How did you figure out how to use the client.QueryIndex function? I couldn't find any documentation anywhere for it.. Thanks – LocustHorde Jan 16 '13 at 13:41
  • 1
    Looks like you already got your answer. Awesome! The documentation for the Neo4jClient is really "limited" right now so I, and I assume everyone else, had to spend hours searching this site as well the rest of the internet looking at working examples. Luckily for us, there are a lot of very helpful people here who have already been through it! – Brent Barbata Jan 16 '13 at 18:14
  • thank you, I am learning for myself, but it is a lot of trial and error.. I was thinking of contributing to the wiki once I understood the basic stuff.. Thanks for answering! – LocustHorde Jan 16 '13 at 19:06
  • 1
    I have the same idea about the wiki. – Brent Barbata Jan 16 '13 at 19:12

1 Answers1

6

In your CreateRelationship call you will need to use the node references, not the nodes, so:

client.CreateRelationship(departure.Reference, new flys_toRelationship(arrival.Reference) { flightNumber = "4" });

The reason why your initial creation code works and this didn't is because Create returns you a NodeReference<Airport> (the var is hiding that for you), and the QueryIndex returns a Node<Airport> instance instead.

Neo4jClient predominantly uses NodeReference's for the majority of its operations.

The second error you had was just related to not using the .Reference property as it couldn't determine the types, when you use the .Reference property that error will go away as well.

Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
  • I can't thank you enough for the answer as well as the very helpful explanation. This had been driving me crazy all day! – Brent Barbata Jan 11 '13 at 17:38
  • I found this blog and code to get me through: http://blog.micic.ch/net/using-neo4j-graph-db-with-c-net HTH – LosManos Feb 27 '13 at 10:43