public async Task<IHttpActionResult> PutBook(int id, Book book) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != book.Id) { return BadRequest(); } db.Entry(book).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostBook(Book book) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Books.Add(book); await db.SaveChangesAsync(); //Load author name by accessing book with Entry(), finding the nav property //with Reference(), then loading it to the db with Load() db.Entry(book).Reference(x => x.Author).Load(); var dto = new BookDTO() { ID = book.Id, Title = book.Title, AuthorName = book.Author.Name }; return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto); }