Cannot deserialize datetime property Neo4j using C# client

一笑奈何 提交于 2019-12-12 13:09:41

问题


I'm trying to get strongly typed objects back out of Neo4j using the C# client. This all works until I add a DateTime property.

I've successfully inserted data into the Neo4j database and I can see it using the console. I can also query the data, but I can't return any strongly typed objects because the deserialization seems to fail.

I'm using parameters to insert the data:

_graphClient.Cypher
.WithParams(new
{
    id = node.Id,
    createdAt = node.CreatedAt,
    lastModified = node.LastModified              
})
.Create("(c { " +
    "Id: {id}, " +
    "CreatedAt: {createdAt}, " +
    "LastModified: {lastModified} } )")

My query to get the data is pretty basic:

nodes = _graphClient.Cypher
.Match("(n)")
.Return((n) => n.As<NeoObject>()).Results.ToList();

But then I receive an error...

The log file states the following:

Parameter name: content ---> Newtonsoft.Json.JsonReaderException: Could not convert string to DateTime: 17-9-2015 21:57:14 +00:00. Path 'a', line 1, position 32.

The data looks like this (entry from log):

"data" : {
  "Id" : 31,
  "LastModified" : "2015-09-17T21:57:14Z",
  "CreatedAt" : "2015-09-17T21:57:14Z",
}

My c# type definitions:

public class NeoObject
{
    public int Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime LastModified { get; set; }
}

or

public class NeoObject2
{
    public int Id { get; set; }
    public DateTime? CreatedAt { get; set; }
    public DateTime? LastModified { get; set; }
}

回答1:


If I recall correctly you need to use a DateTimeOffset type as your property.

public class NeoObject
{
    public int Id { get; set; }
    public DateTimeOffset CreatedAt { get; set; }
    public DateTimeOffset LastModified { get; set; }
}

Edit This used to be the case but it appears that a recent update added support for DateTime object types. what version of Neo4jClient are you using? Have you tried DateTimeOffset?



来源:https://stackoverflow.com/questions/33423174/cannot-deserialize-datetime-property-neo4j-using-c-sharp-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!