Neo4j - Neo4jClient - 从Cypher结果中反序列化。

问题描述 投票:0回答:1

我有一个基本的Neo4j DB,我正在想办法把数据从它变成.Net Core下的C#。我在4.03的DB上使用bolt连接。Windows 10。我可能还没有掌握图形数据库,但我正试图建立一个包含Position的Person对象,其中包含Skill和Ranks。

这段代码返回6个Person的实例(我有6个实例,但这是个小问题,我有点担心

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

人物类

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

改变结果格式,尝试构建复合对象,但只从Person开始(已添加.Results以获得异常),我得到一个Type初始化器不可调用。异常。

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;

给了我下面的异常。挖掘异常意味着我需要一个无参数的构造函数,我添加了。 现在有点迷茫了。

  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
1个回答
0
投票

这是因为我使用的日期时间而导致的失败。数据库中的日期时间的格式如下(包括时区)"1939-06-23T01:00:00+01:00"。把结尾的 +01:00 删掉,它就能在 Person 对象上工作了,其日期时间为

public DateTime BirthDate { get; set; }

在对象类中使用Neo4j.Driver.V1允许我指定一个ZonedDateTime。这与包含时区的原始日期格式是一致的。

public ZonedDateTime BirthDate { get; set; }

非常感谢克里斯,如果没有你的评论,我将会在这里跌跌撞撞地走很久。

© www.soinside.com 2019 - 2024. All rights reserved.