Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class Book
  2. {
  3. public int BookId { get; set; }
  4. public string Title { get; set; }
  5. public Author Author { get; set; }
  6. public ICollection<BookCategory> BookCategories { get; set; }
  7. }
  8. public class Category
  9. {
  10. public int CategoryId { get; set; }
  11. public string CategoryName { get; set; }
  12. public ICollection<BookCategory> BookCategories { get; set; }
  13. }
  14. public class BookCategory
  15. {
  16. public int BookId { get; set; }
  17. public Book Book { get; set; }
  18. public int CategoryId { get; set; }
  19. public Category Category { get; set; }
  20. // NOT IN ALL THE EXAMPLES: public int ID {get; set;}
  21. }
  22.  
  23. modelBuilder.Entity<BookCategory>()
  24. .HasKey(bc => new { bc.BookId, bc.CategoryId });
  25. modelBuilder.Entity<BookCategory>()
  26. .HasOne(bc => bc.Book)
  27. .WithMany(b => b.BookCategories)
  28. .HasForeignKey(bc => bc.BookId);
  29. modelBuilder.Entity<BookCategory>()
  30. .HasOne(bc => bc.Category)
  31. .WithMany(c => c.BookCategories)
  32. .HasForeignKey(bc => bc.CategoryId);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement