How do I tell RavenDB to ignore a property, but not a WebAPI?

I have a property that I do not want to store in RavenDB. If I add the JsonIgnore attribute, RavenDB will ignore the penalty, but then there will be WebApi. I still want WebApi to serialize the data to the web client.

How do I tell RavenDB to ignore a property, but does it still have WebApi serialization?

+8
asp.net-web-api ravendb
source share
1 answer

In RavenDB 2.0

using Raven.Imports.Newtonsoft.Json public class Foo { [JsonIgnore] public string Bar { get; set; } } 

Since it uses a built-in copy of raven for json.net - WebApi will not receive the attribute.

In RavenDB 1.0 or 2.0

You can customize json serialization of your object directly using a custom json contract recognizer.

 public class CustomContractResolver : DefaultRavenContractResolver { public CustomContractResolver(bool shareCache) : base(shareCache) { } protected override List<MemberInfo> GetSerializableMembers(Type objectType) { var members = base.GetSerializableMembers(objectType); if (objectType == typeof(Foo)) members.RemoveAll(x => x.Name == "Baz"); return members; } } 

Connect it to the document repository during initialization:

 documentStore.Conventions.JsonContractResolver = new CustomContractResolver(true); documentStore.Initialize(); 

Since it is not connected anywhere, it will only affect RavenDB. Customize it as you like, according to your needs.

+14
source share

All Articles