2012-05-03 58 views
16

我有一個文檔模型存儲在RavenDB中,但我不想存儲計算的屬性。我如何告訴RavenDB忽略這個屬性?告訴RavenDB忽略一個屬性

在下面的示例中,我不想存儲Duration

public class Build 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public DateTime StartedAt { get; set; } 
    public DateTime FinishedAt { get; set; } 

    public TimeSpan Duration { get { return StartedAt.Subtract(FinishedAt); }} 
} 

回答

24

[JsonIgnore]這樣就裝點Duration屬性:

public class Build 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public DateTime StartedAt { get; set; } 
    public DateTime FinishedAt { get; set; } 

    [Raven.Imports.Newtonsoft.Json.JsonIgnore] 
    public TimeSpan Duration { get { return StartedAt.Subtract(FinishedAt); }} 
} 

查看更多在這裏:http://ravendb.net/docs/client-api/advanced/custom-serialization

+1

附註:如果這個類是-another-項目(如AwesomeNamespace.Core。 ),那麼這個其他項目需要nuget包Newtonsoft.Json或RavenDb.Client。基本上,這個屬性來自Newtonsoft.Json庫。這可能會在未來發生變化,但在我寫這篇評論的時候..這就是得分。 –

+9

「使用RavenDB的第2版,您需要使用Raven.Imports.Newtonsoft.Json命名空間中的屬性,而不是Newtonsoft.Json命名空間,Newtonsoft.Json命名空間將被忽略。」 - 從關於上述鏈接帖子的評論中採取。 – ChadT

+1

RavenDB 3:使用'[Newtonsoft.Json.JsonIgnore]'而不是'Raven.Imports.Newtonsoft.Json'命名空間。看起來,他們已經改變了它,因爲@ ChadT評論了v2。 –