This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I have the situation where I want to eager load a fairly large number of entities and return them from a .NET WebAPI in JSON format so that they can be cached the other end. I've added aggregate names to my lightspeed model and done a Linq query "WithAggregate" which is returning the correct data. However when this is returned by the API it has lost the eager loaded related data, ie only the top level entities are returned. Any suggestions? it must be something to do with how the API is serialising the objects but was wondering if there is any way to tell it to serialise everything. Thanks Matt |
|
|
What serializer are you using? You will need to have it traverse the associations.
|
|
|
It's just the built in .NET serializer, so in the WebApiConfig we have: public static class WebApiConfig
{ |
|
|
Right, this should mean you are using JSON.NET as that is the default used by WebAPI. Try decorating your class with [DataContract] and [DataMember] attributes and include [DataMember] attributes on your associations as required. You will need to be careful though not to include DataMember on both sides of an association though otherwise you will end up with a circular reference issue when serializing. If that isnt practical you can use a custom contract resolver to indicate what properties should be serialized and switch these depending on the root object being serialized - this NuGet package https://github.com/ErikSchierboom/JsonDotNetCustomContractResolvers makes dealing with contract resolvers a bit easier.
|
|
|
Thanks for your reply. We've decorated the relevant classes using the Collection Attributes and Backreference Collection Attributes, so all of the data is now being serialized correctly which is great. However, when the JSON is received from the API we're now having problems de-serializing it. The forward references are working but the back references are failing leaving them as null. |
|
|
I dont think this is possible since you are not serialising the back references because you cant include them without running into a circular reference issue. What are your consumer going to be looking to do with the entities remotely, do they actually need the back-references explicitly defined?
|
|