Neo4j - Neo4jClient - Deserialize from Cypher Results

0

I have a basic Neo4j DB and I'm getting my head around getting data from it into C# under .Net Core. I am using the bolt connection on a 4.03 DB. Windows 10. I may have not grasped Graph Databases yet, but I am trying to build a Person object containing Positions which contain Skill and then Ranks

This code returns 6 instances of Person (A slight concern that I have 6 but thats a minor problem)

var cv = client.Cypher.Match("(person:Person {UserNumber:5})-[:HAS]->(position:Position)<-[:WITHIN]-(skill:Skill)<-[:ON]-(rank:Rank)")
           .Return(person => person.As<Person>());

The Person class

    public class Person
    {
        public Person() {
            RankingProfile = new List<decimal>();
            EmailConfirmed = false;
        }
        …lots of variables (dates, string decimals, boolean)
        public List<decimal> RankingProfile { get; set; }
    }

Changing the result format to try and build the composite object but starting with just Person (Have added .Results to get the exception) I get a Type initializer was not callable. Exception.

var cv = client.Cypher.Match("(person:Person {UserNumber:5})-[:HAS]->(position:Position)<-[:WITHIN]-(skill:Skill)<-[:ON]-(rank:Rank)")
            .Return(person => new {
                Person = person.As<Person>()
            }).Results;

Gives me the exception below. Diggining into the exception implied I needed a parameterless constructor which I added. Bit lost now.

  System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=Neo4jClient
  StackTrace:
   at Neo4jClient.BoltGraphClient.Neo4jClient.IRawGraphClient.ExecuteGetCypherResults[TResult](CypherQuery query)
   at Neo4jClient.Cypher.CypherFluentQuery`1.get_Results()
   at SonOFMatrix.Controllers.CVController.Get() in C:\Users\Controllers\CVController.cs:line 29
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

  This exception was originally thrown at this call stack:
    [External Code]

    Inner Exception 1:
    MemberAccessException: Type initializer was not callable.
        StackTrace  "   
        at System.Reflection.RuntimeConstructorInfo.ThrowNoInvokeException()
        at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) 
        at Neo4jClient.StatementResultHelper.ConstructNew[T]()  
at Neo4jClient.StatementResultHelper.Parse[T](IRecord record, String identifier, IGraphClient graphClient)" string
c#
cypher
neo4jclient
asked on Stack Overflow Apr 26, 2020 by TimH

1 Answer

1

This was failing because of the datetime I was using. The datetime in the database had the following format (it included timezone) "1939-06-23T01:00:00+01:00". Chopping the +01:00 off the end got it working with the Person object with a datetime as

public DateTime BirthDate { get; set; }

using Neo4j.Driver.V1 in the object class allowed me to specify a ZonedDateTime. Which worked with the original date format that included the timezone

public ZonedDateTime BirthDate { get; set; }

Thanks very much Chris without your comments I would have stumbled around a lot longer.

answered on Stack Overflow May 3, 2020 by TimH

User contributions licensed under CC BY-SA 3.0