You can generate the JSON result in your controller as follows:
public JsonResult Person(int id) { var person = PersonRepository.FindByID(id); var result = new { Id = person.Id, Name = person.Name }; return Json(result); }
This will limit the DTO, which is serialized, to contain only the desired values.
Edit: As a paratic answer to your question; you can create a simpler PersonViewModel (DTO) class to which you can map properties. As John Saunders noted in his Automapper answer, this is a good way to simplify copying property values ββfrom an EF Person instance:
A modified action method might look like this:
public JsonResult Person(int id) { var person = PersonRepository.FindByID(id); var dto = Mapper.Map<Person, PersonViewModel>(person); return Json(dto); }
The only other option I can think of is to use reflection to modify the DataMemberAttributes in the Person object to suppress the EntityKey property.
source share