many to many relationship in ef core 5 code example

Example 1: many to many ef core

public class Book{    public int BookId { get; set; }    public string Title { get; set; }    public Author Author { get; set; }    public ICollection<BookCategory> BookCategories { 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; }}

Example 2: one to many relationship ef core

// 1:M
// ONE company has MANY employees

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Employee> Employees { get; set; }
}
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Company Company { get; set; }
}

Example 3: many to many ef core

public class Book{    public int BookId { get; set; }    public string Title { get; set; }    public Author Author { get; set; }    public ICollection<Category> Categories { get; set; }}   public class Category{    public int CategoryId { get; set; }    public string CategoryName { get; set; }    public ICollection<Book> Books { get; set; }}

Tags:

Misc Example