Я пытаюсь получить все машины у владельцев. Один владелец может иметь несколько автомобилей, и один автомобиль может быть зарегистрирован на одного владельца.
Если я наберу такой запрос, я получу ожидаемый результат:
public List<Owner> Get()
{
var ownerWithCars = db.Owner.Include(o => o.Cars).AsNoTracking().ToList();
return ownerWithCars;
}
Но этот запрос не работает :
public List<Owner> Get()
{
var cars = from o in db.Owners join c in db.Cars on o.Id equals c.OwnerId into cars
select new Owner() {Id = o.Id, Address = o.Address, Cars = cars.ToList()};
return cars;
}
Если я запускаю второй запрос, я получаю следующую ошибку:
"ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
Я добавил следующее к своему global.asax
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters
.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);