SHARE
TWEET

Untitled

a guest Jun 25th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top