I have been playing with MVC 4 Web API with EF Code First 5. I have these POCOs:
public class Vehicle
{
public int Id { get; set; }
public int VehicleMakeId { get; set; }
public int Model { get; set; }
public int Year { get; set; }
public int FuelType { get; set; }
public int NoOfSeats { get; set; }
public virtual VehicleMake VehicleMake { get; set; }
}
public class VehicleMake
{
public int Id { get; set; }
public string Description { get; set; }
}
And a web API controller call: ‘http://localhost:63779/api/’vehicles and a GET that looks like:
public IEnumerable<Vehicle> Get()
{
return Uow.Vehicles.GetAll().OrderBy(v => v.VehicleMake.Description);
}
I can return JSON results to the browser:
{
id: 1,
vehicleMakeId: 1,
model: 1,
year: 2004,
fuelType: 2,
noOfSeats: 1,
vehicleMake: null
}
Notice vehicleMake is null. However, when stepping through the code the object does have the correct values.
Is it possible to see the JSON response for the object? Is it even desirable in I guess what is a lookup property?
I realise there is quite a bit of code missing here but hope there is enough for someone to give an explanation of what’s going on here. If not I can post more info.
Thanks
Your VehicleMake is lazy loaded. When you look on it with the debugger it is loaded immediatley, so you need to include it in the query: