【问题标题】:Tell RavenDB to ignore a property告诉 RavenDB 忽略一个属性
【发布时间】:2012-05-03 23:58:26
【问题描述】:

我有一个文档模型要存储在 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); }}
}

【问题讨论】:

    标签: c# ravendb


    【解决方案1】:

    只需像这样用[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]
        //[Newtonsoft.Json.JsonIgnore] // for RavenDB 3 and up
        public TimeSpan Duration { get { return StartedAt.Subtract(FinishedAt); }}
    }
    

    在此处查看更多信息:http://ravendb.net/docs/client-api/advanced/custom-serialization

    【讨论】:

    • 旁注:如果这个类在 -another- 项目中(例如 AwesomeNamespace.Core),那么这个其他项目需要 nuget 包 Newtonsoft.Json 或 RavenDb.Client。基本上,这个属性来自 Newtonsoft.Json 库。这可能会在未来发生变化,但在我写这篇评论的时候......这就是分数。
    • "使用 RavenDB 版本 2,您需要使用来自 Raven.Imports.Newtonsoft.Json 命名空间而不是 Newtonsoft.Json 命名空间的属性。Newtonsoft.Json 命名空间将被忽略。" -- 取自上述链接帖子中的 cmets。
    • RavenDB 3:使用 [Newtonsoft.Json.JsonIgnore] 而不是 Raven.Imports.Newtonsoft.Json 命名空间。似乎,自从@ChadT 对 v2 发表评论以来,他们已经改变了它。
    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多