1

我正在使用此处找到的示例试用Neo4jClient用于Neo4j图形数据库。

在以下相当简单的代码中:

var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

var myNodeReference = client.Create(new MyNode { Foo = "bar" });
var myNode = client.Get<MyNode>(myNodeReference);

在最后一行 ( .Get) 抛出以下错误:

An item with the same key has already been added.

(即使Get是第一个也是唯一的方法,也会引发相同的错误,并且我正在使用之前创建的一些现有密钥获取节点)。

查看堆栈跟踪后,我发现它与 Neo4jClient 相关,而不是 Neo4j db,因为它似乎是添加到 Dictionary 时引发的错误:

 at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Neo4jClient.Deserializer.CommonDeserializerMethods.GetPropertiesForType(Type objType) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CommonDeserializerMethods.cs:line 344
   at Neo4jClient.Deserializer.CommonDeserializerMethods.Map(Object targetObject, JToken parentJsonToken, CultureInfo culture, IEnumerable`1 typeMappings, Int32 nestingLevel) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CommonDeserializerMethods.cs:line 228
   at Neo4jClient.Deserializer.CommonDeserializerMethods.CreateAndMap(Type type, JToken element, CultureInfo culture, IEnumerable`1 typeMappings, Int32 nestingLevel) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CommonDeserializerMethods.cs:line 210
   at Neo4jClient.Deserializer.CommonDeserializerMethods.SetPropertyValue(Object targetObject, PropertyInfo propertyInfo, JToken value, CultureInfo culture, IEnumerable`1 typeMappings, Int32 nestingLevel) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CommonDeserializerMethods.cs:line 132
   at Neo4jClient.Deserializer.CommonDeserializerMethods.Map(Object targetObject, JToken parentJsonToken, CultureInfo culture, IEnumerable`1 typeMappings, Int32 nestingLevel) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CommonDeserializerMethods.cs:line 234
   at Neo4jClient.Deserializer.CustomJsonDeserializer.Deserialize[T](RestResponse response) in c:\TeamCity\buildAgent\work\460e89b30f53245b\Neo4jClient\Deserializer\CustomJsonDeserializer.cs:line 59
   at RestSharp.RestClient.Deserialize[T](IRestRequest request, RestResponse raw)

如果相关,这些是来自 NuGet 的包版本:
<package id="Neo4jClient" version="1.0.0.397" targetFramework="net40-Client" />
<package id="Newtonsoft.Json" version="4.0.8" targetFramework="net40-Client" />
<package id="RestSharp" version="102.7" targetFramework="net40-Client" />

我在这里做错了什么?

编辑 MyNode类:

[JsonObject]
public class MyNode
{
    [JsonProperty("Bar")]
    public string Foo { get; set; }

    [JsonIgnore]
    public string Bar { get; set; }
}
4

1 回答 1

3

除非您真的需要这样做,否则我会取下[JsonProperty("Bar")]钻头,因为这就是造成麻烦的原因。反序列化器无法区分实际属性“Bar”和 JsonProperty“Bar”

如果你把它取下来,你的代码就可以正常工作。

如果你使用 Json 的东西,你可以创建另一个 Node 对象:

public class OtherNode { public string Bar { get;set;} }

并反序列化为:

var nodeReference = client.Create(new MyNode { Foo = "blah" });
var retrieved = client.Get<OtherNode>(nodereference);

这将起作用。

于 2012-07-25T14:47:06.377 回答