I would like to embed foreign key objects into my .NET JSON responses in my API. I come from a Rails background and formatting the JSON response to include any found foreign key records is fairly simple. However, I can't seem to find the way to do this. I have a foreign key relationship set up like the below models:
public class Person
{
[Key]
public int id {get; set;}
public string name {get; set;}
}
public class House
{
[Key]
public int id {get; set;}
public string address {get; set;}
[Required]
public int personId {get; set;}
[ForeignKey("peronsId")]
public Person person {get; set;}
}
However, when I run a GET request, I get the house in the form:
{
"id":1,
"address":"Middle of Nowhere",
"personId":3
}
When what I want is:
{
"id":1,
"address":"Middle of Nowhere",
"person":{
"id":3,
"name":"Some Dude"
}
}
Or some similar structure. How can I accomplish this in .NET?
Using .NET 4.5 Web API 2 with Entity Framework in C#.