.NET Core — EF Many-To-Many
In most applications, most of the time we discover that our business logic requires some sort of an entity-relationship to fulfill the needs. sometimes we find out the entity's cardinality is more than one on both ends and that’s where the Many-To-Many relationship is required.
Today, I will walk you through how to configure Many to many relationships with EF-Core and .NET core.
LET’S GET INTO IT!!!.
1-Adding the Entities.
a- Product
public class Product{public int ID { get; set; }public string Name { get; set; }public string Description { get; set; }public ICollection<ProductCategory> Categories { get; set; }}
b- Category
public class Category{public int ID { get; set; }public string Name { get; set; }public ICollection<ProductCategory> Products { get; set; }}
c- Linking Table (ProductCategory)
public class ProductCategory{[Key]public int ID { get; set; }public int ProductID { get; set; }public Product Product { get; set; }public int CategoryID { get; set; }public Category Category { get; set; }}
If you have worked with Many-To-Many relationships in ASP.NET, you might ask yourself why we need a linking table here? well, in ASP.NET it used to figure out the end relationship on its own. The reason why we need to specify a linking table in the .NET core is that EF Core does not support this feature but, Microsoft announced they will include this feature in .NET 5.
2-Adding the Context
a- Installing required

The packages version might subject to change based on your framework version (Make sure to check the version before downloading)
b- Creating the DbContext
public class ProductContext : DbContext{public DbSet<Product> Products { get; set; }public DbSet<Category> Categories { get; set; }public DbSet<ProductCategory> ProductCategories { get; set; }public ProductContext(DbContextOptions<ProductContext> options): base(options){}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.Entity<ProductCategory>().HasKey(p => new {p.CategoryID,p.ProductID});modelBuilder.Entity<ProductCategory>().HasOne(p => p.Product).WithMany(c => c.Categories).HasForeignKey(p => p.ProductID);modelBuilder.Entity<ProductCategory>().HasOne(p => p.Category).WithMany(c => c.Products).HasForeignKey(p => p.CategoryID);}}
As you can see we’ve added the Data sets we require and also we’ve overridden the OnModelCreating to specify the end relationship to our corresponding entites. we’re saying ProductCategory has composite keys of Product and Category also, we’re specifying the Product and Category respective relationship in here as well.
Now, with the help of the linking table, we can add and update any relationship between Product and Category. The Many-to-Many operations will happen in ProductCaetgory while the other CRUDs will happen for the related entity.
There are many ways and techniques to specify the types of relationships and this one way of many others out there. As longs as the technique suites your business aims and the application’s architecture that’s what matters the most.
Hope you've enjoyed reading this piece of information.
Connect with me on LinkedIn