繁体   English   中英

使用C#中的Neo4jClient在Neo4j中创建节点之间的关系

[英]Creating Relationships Between Nodes in Neo4j with Neo4jClient in C#

我正在使用.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

有任何想法吗?

CreateRelationship调用中,您将需要使用节点引用,而不是节点,因此:

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

您的初始创建代码工作的原因并非如此,因为Create返回NodeReference<Airport>var为您隐藏),而QueryIndex返回Node<Airport>实例。

Neo4jClient主要使用NodeReference进行大部分操作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM