How to implement many to many relationship in ASP.NET CORE WEB API

So I have 4 classes:

    public class Book
    {
      public int BookId { get; set; }
      public string Title { get; set; }
      public Author Author { get; set; }
      public int AuthorId { get; set; }
      public ICollection<BookCategory> BookCategories { get; set; }
    }  

    public class Author
    {
      public int AuthorId { get; set; }
      public string Name { get; set; }
      public string Surname { get; set; }
      public ICollection<Book> Books { get; set; }
    }  

    public class Category
    {
      public int CategoryId { get; set; }
      public string CategoryName { get; set; }
      public ICollection<BookCategory> BookCategories { get; set; }
    }

    public class BookCategory
    {
      public int BookId { get; set; }
      public Book Book { get; set; }
      public int CategoryId { get; set; }
      public Category Category { get; set; }
    }

I want to write Function for [HttpGet] request, so that it returns JSON like this:

[
  {
     "bookId": "1",
     "title": "Book Title 1",
     "authorName": "Author Name 1",
     "aurhorSurname": "Author Surname 1",
     "categories": ["category1", "category2"]
  },
  {
     "bookId": "2",
     "title": "Book Title 2",
     "authorName": "Author Name 1",
     "aurhorSurname": "Author Surname 1",
     "categories": ["category2", "category3"]
  },
  {
     "bookId": "3",
     "title": "Book Title 3",
     "authorName": "Author Name 1",
     "aurhorSurname": "Author Surname 1",
     "categories": ["category1", "category4"]
  },
]

I know how to handle One-to-many relathionships, and I created API for author and book where every author has an array of books that he wrote. But this join table (BookCategory) confuses me.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For the returned json format, you can get all the associated data through Include and ThenInclude first.

Then use the Select method and new objects to form the returned data structure:

        public IActionResult ReturnJson()
        { 
            var data = _context.Book.Include(x => x.Author)
                .Include(x => x.BookCategories).ThenInclude(x => x.Category)
                .Select(x => new
                {
                    bookId = x.BookId,
                    title = x.Title,
                    authorName = x.Author.Name,
                    authorSurname = x.Author.Surname,
                    categories = x.BookCategories.Select(k => k.Category.CategoryName).ToList()
                });
            return Json(data);
        }

Here is the returned data:

How to implement many to many relationship in ASP.NET CORE WEB API


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x