问题
Please bear with me I'm new to this: I'm currently using the .Net neo4jClient. Currently I have a Share node and a Customer node. I'm creating a relationship CustomerOwnsShare between them and persisting it.
Here's my Relationship class
public class CustomerOwnsShare :
Relationship,
IRelationshipAllowingSourceNode<Customer>,
IRelationshipAllowingTargetNode<Share>
{
public CustomerOwnsShare(NodeReference targetNode)
: base(targetNode)
{
}
public int Quantity { get; set; }
public float CostPerShare { get; set; }
public string DateOfPurchase { get; set; }
public string ShareSymbol { get; set; }
public const string TypeKey = "CUSTOMER_OWNS_SHARE";
public override string RelationshipTypeKey
{
get { return TypeKey; }
}
}
Now to retrieve a list of Relationships back from the Database I'm using Linq as below
IEnumerable<RelationshipInstance> relationshipInstances =
graphClient.RootNode.In<Customer>(CustomerBelongsTo.TypeKey, c => c.Email == email)
.OutE(CustomerOwnsShare.TypeKey)
But this returns me RelationshipInstance object which doesn't have the data that i need(Quantity, CostPerShare, etc.).
RelationshipInstance exposes a RelationshipReference Object, but even that doesn't help me retrieve my actual Relationship Object. On digging a little deeper i see that i can execute Raw gremlin query as below
graphClient.ExecuteGetAllRelationshipsGremlin<>()
but the function signature of that also returns me an IEnumerable of RelationshipInstance.
Any ideas or suggestions on how i can retrieve my actual persisted Relationship object with it's data ??
Thanks in Advance
回答1:
Sorry for the time to get this to you, what you actually want is a 'RelationshipInstance<CustomerOwnsShare>
'...
So, let's pretend I have the following setup:
Root(0) -[]-> User(1) -[CUSTOMER_OWNS_SHARE]-> MSFT(2)
The numbers in brackets are the neo4j references. The query I would perform using neo4jclient is:
var results = graphClient.ExecuteGetAllRelationshipsGremlin<CustomerOwnsShare>("g.v(2).inE", null);
var quant = results[0].Data.Quantity; //etc
Now, if you just copy / paste this, you're going to get an error:
'CustomerOwnsShare' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TData' in the generic type or method 'Neo4jClient.GraphClient.ExecuteGetAllRelationshipsGremlin<TData>(string, System.Collections.Generic.IDictionary<string,object>)'
Which is a pain, the way around this is to put a parameterless constructor into your CustomerOwnsShare class:
[EditorBrowsable(EditorBrowsableState.Never)]
public CustomerOwnsShare() : base(0) { }
This is fine for you, as the TargetNode will be set by the deserialiser. You do want to make sure that you don't use that constructor yourself though. The 'EditorBrowsable' will prevent external assemblies from seeing it, but unfortunately won't do anything for any code in the same assembly, so you might want to mark it as:
[Obsolete]
as well, just to act as a reminder to yourself.
来源:https://stackoverflow.com/questions/12491221/how-do-i-retrieve-a-relationship-in-neo4j-graph-database