We’re using Entity Framework to query a complex object graph using the .Include(“”) method to eager load the results, then sending it to the client with the WebApi. We’ve overridden the JavaScript Serializer in the Application_Start of Global.asax.cs of our MVC application to avoid circular dependencies. These occurred because of the Code First classes that were generated from the EF Power Tools had references to each other. It also created really large JSON structures that we didn’t need all the properties filled out. To avoid this, we added the [JsonIgnore] attribute to properties we don’t need.
// Add this in the Application_Startup of the Global.asax.cs
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// add the attribute, using partial classes and MetaData
// Entity Framework class
public partial class Entity
{
public IEnumerable<string> Attributes { get; set; }
}
[MetadataType(typeof(EntityMetaData))]
public partial class Entity
{
}
public class EntityMetaData
{
// I don’t need these on the client side
[JsonIgnore]
public IEnumerable<string> Attributes { get; set; }
}
Now the JSON is much cleaner.
http://james.newtonking.com/projects/json/help/index.html?topic=html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor.htm