2012-07-24 88 views
1

我正在試用Neo4jClientNeo4j圖形數據庫,使用發現的示例hereNeo4jClient - 如何獲取節點?

在下面很簡單的代碼:

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的分貝,因爲它似乎是添加到字典時所引發的錯誤:

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; } 
} 
+0

你能分享你的'MyNode'類嗎? – 2012-07-25 10:36:20

+0

更新 - 在帖子末尾添加了... – veljkoz 2012-07-25 14:05:07

回答

3

除非你真的需要做到這一點,我會脫下[JsonProperty("Bar")]位,因爲這是什麼原因造成的麻煩。解串器無法區分實際屬性'Bar'和JsonProperty'Bar'

如果你把它關掉,你的代碼將會正常工作。

如果你想使用JSON東西,你可以創建另一個Node對象:

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

和deserialise成:

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

,這將正常工作。

+0

yeap,就這麼簡單;)謝謝 – veljkoz 2012-07-25 18:57:33