Bre*_*ata 5 c# asp.net neo4j neo4jclient
我正在使用.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; }
}
}
Run Code Online (Sandbox Code Playgroud)
主要
// 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" });
Run Code Online (Sandbox Code Playgroud)
但问题是,当我想要添加与现有节点的关系时.假设我有一个只包含两个节点(机场)的图表,比如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" });
Run Code Online (Sandbox Code Playgroud)
我收到的两个错误如下:
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
有任何想法吗?
在您的CreateRelationship通话中,您将需要使用节点引用,而不是节点,因此:
client.CreateRelationship(departure.Reference, new flys_toRelationship(arrival.Reference) { flightNumber = "4" });
Run Code Online (Sandbox Code Playgroud)
你的初始创建代码工作的原因并不是因为Create返回你NodeReference<Airport>(var隐藏了你的那个),而是QueryIndex返回一个Node<Airport>实例.
Neo4jClient主要NodeReference用于其大部分操作.
您遇到的第二个错误与不使用.Reference属性有关,因为它无法确定类型,当您使用该.Reference属性时,错误也会消失.
| 归档时间: |
|
| 查看次数: |
3272 次 |
| 最近记录: |