Search code examples
entity-frameworkjson.netasp.net-web-api

How to ignore navigation properties when serializing


I'm using ASP.NET Web API with Entity Framework. I changed my default serializer to JSON.NET (cause the default DataContractSerializer didn't work at all with EF). Now it's better (it's working at least), however still not perfect. After sending the GET request I obtain all properties from one table plus lot's of data from navigation properties (so basically all data from other entities which have relation with entity I want to obtain...). How can I make it serializing only fields from this entity and not navigation properties?

Thanks for help


Solution

  • Try to disable lazy loading.

    The Json serializer is iterating through the properties of your entity to serialize them and therefore also is calling the getter of the navigation properties. Calling the getter of a navigation property = triggering lazy loading. Loading was delayed a bit but only until the serializer reached the navigation properties and caused an additional database query to fetch the child property values which then have been serialized as well.