I have 3 projects in my solution: DataModel (EF), DAL which works with Entities from DataModel and MVC Web API.
There are only 2 very simple entities: Person, Address with 3 simple fields in each of them and Person has the Address field (so these 2 entities are linked)
In my DAL I have a method that returns me the list of all the Persons, the content is very simple: return context.Person.ToList();
Now In my Web API I'm simply calling for the GetPersons() method and trying to return it. And here I get a strange error message:
"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.",...
When I debug I can see that I do have the data from my GetPersons method. I also googled and found the only solution: I should have added the following lines into my start config:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
But it does not help.
I also tried to populate the list manually without using the database: and in this case it works.
I have a strong impression that it has something to do with EF but I don't know exactly what's that.