Last post Mar 05, 2016 11:17 PM by lolzoid
Mar 05, 2016 06:26 PM|lolzoid|LINK
I have extracted a database model from an existing database (as code first). I noticed that when I created an api for each table it produced an absolutely insane amount of results. Even when I had about 43 insert lines worth of data it produced I would estimate
maybe several tens of millions worth of JSON lines in JSONView (I say estimated because after waiting 2 minutes I had to halt the progress. I tried it in POSTMAN too and it did the same thing!)
I have been using DTOs but I am having difficulty producing a more complex JSON response. At the moment the users of the API have to make multiple calls and combine the data themselves. Maybe that is fine doing it like that but I am not sure if that is inefficient
Does someone have a simple example of how to combine 2 models in a DTO in order to get arrays of data from another table? I tried doing this in the DTO but ended up with the same problem again... millions of results being returned as if were placing the
entire copies of the database data inside single array elements! The one from Mike Wasson is a little too simple.
Mar 05, 2016 11:17 PM|lolzoid|LINK
I have finally got it! With ref to: http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 the solution is to do the same for the
array element as you do with the parent like this:
public IQueryable<BookDTO> GetBooks()
var books = db.Books.Select(b =>new BookDTO()//(corrected mix up of sql styles!)
Id = b.Id,
Title = b.Title,
AuthorName = b.Author.Name,
ExampleItems = db.ExampleItems.Select(ex => new ExampleItemDTO()
Blah = ex.Blah,
}).Where(ex => ex.Id == b.Id).ToList() //assuming it is a List!
I do not know why he does not do a more complicated item example on that page. It seems simple now that I have worked it out but no one is going to want to simply do a basic list like the one he shows. Maybe it is obvious what to do but it is not always
as obvious as it seems.