.Net Web API - Entity Framework - ignore relationships during serialization
asp.net-web-api, entity-framework
Solution
in your return value "select" a new type
return ienumfromedmx.Select(o=> new { id = id, value = value, name = name});
where these are the values that you want to return
if you post some example code it might be easier to give you a more relevant code example
using your book
public Book GetBook(int id) { return books.SingleOrDefault(b => b.Id == id);}
change to
public dynamic GetBook(int id){
return books.SingleOrDefault(b=>b.id == id).Select(new { id = id, Title = Title, price = Price});
}
OR
public object GetBook(int id){
return books.SingleOrDefault(b=>b.id == id).Select(new { id = id, Title = Title, price = Price});
}
OR (for reference sake - I'd use dynamic or object for the api rather than a JsonResult)
public JsonResult GetBook(int id){
return Json(books.SingleOrDefault(b=>b.id == id).Select(new { id = id, Title = Title, price = Price}));
}
note if you're using
public JsonResult GetBook(int id){
return Json(books.SingleOrDefault(b=>b.id == id).Select(new { id = id, Title = Title, price = Price}));
}
and if you're using httpget as opposed to post, then you'd need to use
public JsonResult GetBook(int id){
return Json(books.SingleOrDefault(b=>b.id == id).Select(new { id = id, Title = Title, price = Price},JsonRequestBehavior.AllowGet);
}
Problem
I am creating a ASP.Net Web API to expose an existing database for use with handheld devices. I am also relatively new at Web API, EF, and everything else I'm trying to use on this project :) I want the resulting data transfer to be as lightweight as possible but when the api returns an object, the serialized JSON has 'EntityKey' fields for itself and the rows from other tables that the object has a relationship with. I'm now trying to use the ADO.NET DbContext Template Generator for the model code generation. This gets rid of the EntityKey fields but I still have the relationships showing up in the JSON. All I want are the fields from the object to be serialized and to be able to deserialize JSON into these objects for inserts and updates. Is there a built-in way to do this? What's my best bet?