.NET Core — EF Many-To-Many

Photo by Tamanna Rumee on Unsplash

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.

Photo by Alexander Schimmeck on Unsplash

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

THANK YOU!!

--

--

--

Software developer | Programming and Blockchain enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

TryHackMe-Team

Highly Customizable Network Layer in Swift 5

SQL CRUD Basics Part 4 — Delete.

.NET benchmarking and profiling for beginners

Project Update: Camera Follow

Launching EMR backed SageMaker Notebook via InfraStructure As Code (Boto3, CloudFormation)

Provisioning Freeform Configuration via AWS AppConfig & CloudFormation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ibrahim Jaber

Ibrahim Jaber

Software developer | Programming and Blockchain enthusiast

More from Medium

Build an ASP.NET Core Web API 5.0 with CURD operations using Entity Framework DB first approach.

NET5 Request-Response Logging

Blazor with Auth0, using the Management API

Using Medaitr in Background Services