Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell RavenDB to ignore a property

Tags:

c#

ravendb

I have a document model to store in RavenDB but I don't want to store a calculated property. How do I tell RavenDB to ignore this property?

In the below example I don't want to store 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); }}
}
like image 645
Ben Clark-Robinson Avatar asked May 03 '12 23:05

Ben Clark-Robinson


1 Answers

Just decorate the Duration property with [JsonIgnore] like this:

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); }}
}

See more here: http://ravendb.net/docs/client-api/advanced/custom-serialization

like image 127
Adam Spicer Avatar answered Oct 11 '22 00:10

Adam Spicer