5

我正在使用 .Net Neo4jClient ( http://hg.readify.net/neo4jclient/wiki/Home ) 使用 Neo4j。在我的代码中,节点是机场,关系是航班。

如果我想同时创建节点和关系,可以使用以下代码:

课程

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

主要的

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

然而,问题是当我想向已经存在的节点添加关系时。假设我有一个仅包含两个节点(机场)的图表,例如 SNA 和 EWR,我想添加从 SNA 到 EWR 的关系(航班)。我尝试以下操作,但失败了:

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

我收到的两个错误如下:

1) 参数 1:无法从 'Neo4jClient.Node' 转换为 'Neo4jClient.NodeReference'

2) 无法从用法中推断方法“Neo4jClient.GraphClient.CreateRelationship(Neo4jClient.NodeReference, TRelationship)”的类型参数。尝试明确指定类型参数。

错误所指的方法在以下类中:http ://hg.readify.net/neo4jclient/src/2c5446c17a65d6e5accd420a2dff0089799cbe16/Neo4jClient/GraphClient.cs?at=default

有任何想法吗?

4

1 回答 1

6

在您的CreateRelationship通话中,您将需要使用节点引用,而不是节点,因此:

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

您的初始创建代码有效而无效的原因是因为Create返回了一个NodeReference<Airport>var为您隐藏了它),而是QueryIndex返回了一个Node<Airport>实例。

Neo4jClientNodeReference的大部分操作主要使用 's。

您遇到的第二个错误与不使用该.Reference属性有关,因为它无法确定类型,当您使用该.Reference属性时,该错误也会消失。

于 2013-01-11T09:53:59.333 回答