I have a small Web Api method. How can I map properties in GetBooks method with Automapper? I have tried the solution here .
I have tried
return from c in db.Books select Mapper.Map<BookDTO>(c);
But it didnt work. Here is my complete code;
//GET: api/Books
public IQueryable<BookDTO> GetBooks()
{
var books = from b in db.Books
select new BookDTO()
{
Id = b.Id,
Title = b.Title,
AuthorName = b.Author.Name
};
return books;
}
Book.cs
public class Book
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public int Year { get; set; }
public decimal Price { get; set; }
public string Genre { get; set; }
// Foreign Key
public int AuthorId { get; set; }
// Navigation property
public Author Author { get; set; }
}
BookDTO.cs
public class BookDTO
{
public int Id { get; set; }
public string Title { get; set; }
public string AuthorName { get; set; }
}
thanks in advance.