Many to Many Relationship in Entity Framework Core (Fluent API) Example

When many-to-many relationships occur in a relational database? 

In a relational database, we have 3 types of relationships. They are one-to-one, one-to-many, many-to-many. Many-to-many relations establish when one table has a relationship with many records in another table. 

Many-to-Many Relationship Example In the database

Now, begin with Entity-Relationship Diagram(ERD) design in which many "Post" can have many "Categories". 

Models

1. Post

2. Category

Attributes of Post

  • Id
  • Title
  • Content

Attributes of Category

  • Id
  • Name
Relationship between post and category many posts has many categories.



Convert ERD to a database

Let's convert the entity-relationship diagram to the database.
Here, we have two models and have one relationship, so we need to make another junction table, which is for the relationship between the post and category.

Let's create another model named PostCategory.


Attribute of PostCategory
  • PostId
  • CategoryId


Configure many-to-many relationships in ef core.

Now, Create a model class called Post.

 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace MtoM.Models
{
    public class Post
    {
        public int Id { getset; }
        public string Title { getset; }
        public string Content { getset; }
        public ICollection<Category> Categories { getset; }
    }
}
 
 
 
Let's, create a model called Category.

    
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace MtoM.Models
{
    public class Category
    {
        public int Id { getset; }
        public string Title { getset; }
 
    }
}
 
 
 

Now let us create a Model PostCategory
PostCategory is the junction table that helps to create a many-to-many relationship between models between post and category.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace MtoM.Models
{
    public class PostCategory
    {
        public int PostId { getset; }
        public int CategoryId { getset; }
 
        public Post Post { getset; }
        public Category Category { getset; }
 
 
    }
}

Let's configure many many relationships in ApplicationDbContext using Fluent API.

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MtoM.Models;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace MtoM.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbSet<Post> Posts { getset; }
        public DbSet<Category> Categories { getset; }
 
        public DbSet<PostCategory> PostCategories { getset; }
 
        protected override void OnModelCreating(ModelBuilder mb)
        {
            base.OnModelCreating(mb);
 
           
            mb.Entity<PostCategory>()
              .HasKey(pc => new { pc.PostId, pc.CategoryId });
 
 
        }
 
    }
}



Reactions

Post a Comment

0 Comments