我正在使用.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
有什么想法吗?
答案 0 :(得分:6)
在CreateRelationship
调用中,您需要使用节点引用,而不是节点,因此:
client.CreateRelationship(departure.Reference, new flys_toRelationship(arrival.Reference) { flightNumber = "4" });
你的初始创作代码之所以有效,而这不是因为Create
会给你NodeReference<Airport>
(var
隐藏着你),而{{1} }}返回QueryIndex
实例。
Neo4jClient主要使用Node<Airport>
来进行大部分操作。
您遇到的第二个错误与不使用NodeReference
属性有关,因为它无法确定类型,当您使用.Reference
属性时,错误也会消失。