Reputation: 411
Trying to look at a relationship in a query like this:
var query = _graph.Cypher.Start(
new
{
me = Node.ByIndexLookup("node_auto_index", "id", p.id)
}).Match("me-[r:FRIENDS_WITH]-friend")
.Where((Person friend) => friend.id == f.id)
.Return<FriendsWith>("r");
Here is the FriendsWith class. I can't add a parameterless constructor for FriendsWith, because the base class (Relationship) doesn't have a parameterless constructor.
public class FriendsWith : Relationship,
IRelationshipAllowingSourceNode<Person>,
IRelationshipAllowingTargetNode<Person>
{
public FriendsWith(NodeReference<Person> targetNode)
: base(targetNode)
{
__created = DateTime.Now.ToString("o");
}
public const string TypeKey = "FRIENDS_WITH";
public string __created { get; set; }
public override string RelationshipTypeKey
{
get { return TypeKey; }
}
}
But I get the error "No parameterless constructor defined for this object." when I try to run it. What is the proper way to return a relationship for a query?
Stack trace
at Neo4jClient.Deserializer.CypherJsonDeserializer1.Deserialize(String content) in c:\TeamCity\buildAgent\work\f1c4cf3efbf1b05e\Neo4jClient\Deserializer\CypherJsonDeserializer.cs:line 53
at Neo4jClient.GraphClient.<>c__DisplayClass1d
1.b__1c(Task1 responseTask) in c:\TeamCity\buildAgent\work\f1c4cf3efbf1b05e\Neo4jClient\GraphClient.cs:line 793
at System.Threading.Tasks.ContinuationResultTaskFromResultTask
2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Upvotes: 1
Views: 315
Reputation: 4290
Just deserialize it into a POCO that represents the data structure:
public class FriendsWith
{
public string __created { get; set; }
}
var query = _graph.Cypher
.Start(new {
me = Node.ByIndexLookup("node_auto_index", "id", p.id)
})
.Match("me-[r:FRIENDS_WITH]-friend")
.Where((Person friend) => friend.id == f.id)
.Return(r => r.As<FriendsWith>())
.Results;
You actually don't need the FriendsWith : Relationship, IRelationshipAllowingSourceNode<Person>, IRelationshipAllowingTargetNode<Person>
class at all.
Create relationships using Cypher:
_graph.Cypher
.Start(new {
me = Node.ByIndexLookup("node_auto_index", "id", p.id),
friend = Node.ByIndexLookup("node_auto_index", "id", p.id + 1)
})
.CreateUnique("me-[:FRIENDS_WITH {data}]->friend")
.WithParams(new { data = new FriendsWith { __created = DateTime.Now.ToString("o") } })
.ExecuteWithoutResults();
You'll see more examples on the Neo4jClient wiki. Basically, in this day and age, everything should be Cypher.
Upvotes: 0