Can't get category's books even though the Book entity has a category foreign key?

Viewed 106

I am new to asp.net so easy on me :D, I am builiding a simple CRUD app, I managed to get the lists and routing correctly, but now I want to get a category's book.

So that's my Book & Category Entities :

public class Book
    {
        [Key]
        public int bookId { get; set; }
        public string bookName { get; set; }
        public double bookPrice { get; set; }
        public string bookImageUrl { get; set; }
        [ForeignKey("Category")]
        public int categoryId { get; set; }
        public Category category { get; set; }

    }

public class Category
    {
        [Key]
        public int categoryId { get; set; }
        public string categoryName { get; set; }
        public string categoryDescription { get; set; }
        public List<Book> books { get; set; }
    }

This is my category repository, the second method is the one, I am including the books :

public class CategoryRepository : ICategoryRepository
    {
        private readonly AppDbContext _appDbContext;

        public CategoryRepository(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }

        public IEnumerable<Category> getAllCategories()
        {
            return _appDbContext.Categories;
        }

        public Category getCategoryById(int categoryId)
        {
            return _appDbContext.Categories.Include(category => category.books).FirstOrDefault(category => category.categoryId == categoryId);
        }
    }

This is my CategoryController even though I don't the probleme is here :

private readonly ICategoryRepository _categoryRepository;
        public CategoryController(ICategoryRepository categoryRepository)
        {
            _categoryRepository = categoryRepository;
        }
        public IActionResult categoryBooks(int idCategory)
        {
            Trace.WriteLine("this " + idCategory);
            var booksListViewModel = new BooksListViewModel();
            booksListViewModel.Books = _categoryRepository.getCategoryById(idCategory).books;
            return View(booksListViewModel);
        }
    }

I have one book & one category in the database and that's it :

enter image description here

Any ideas ? Any guiding ?

Any help would be much appreciated.

1 Answers

Since you are asking for a first or default

return _appDbContext.Categories.Include(category => category.books).FirstOrDefault(category => category.categoryId == categoryId);

it returns the first category it found. This is why you have only one record. It is a relational data base. Each record has one bookId and one categoryId and it takes the first record where where is true.

if you want a list books with category you have change query to this

public IEnumerable<Book> getBooksByCategoryId(int categoryId)
        {
 return _appDbContext.Books.Include(c => c.category).Where(category => category.categoryId == categoryId).ToArray();
}
Related