© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3mo ago•
8 replies
Yarden

I'm trying to implement many-to-many relationship inside my database. Not sure what to do next.

beginnerasp.netintermediate
I have 3 tables already:
Users, Coffeeshop and UserCoffeeShops for the many-to-many relationships.

I already added the necessary code:
Inside dbContext:
 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     modelBuilder.Seed(); //A side method to add the constraints

     //Creation of many-to-many relationship:
     modelBuilder.Entity<UserEntity>()   
         .HasMany(x => x.CoffeeShops)    //  Specifies that a User can have many Coffee shops.
         .WithMany(x => x.Users)         //  Specifies that a Coffee shop can have many Users.       
         .UsingEntity(j => j.ToTable("UserCoffeeShops")); //  Explicitly names the join table. (If I don’t specify this, EF Core will)
 }
 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     modelBuilder.Seed(); //A side method to add the constraints

     //Creation of many-to-many relationship:
     modelBuilder.Entity<UserEntity>()   
         .HasMany(x => x.CoffeeShops)    //  Specifies that a User can have many Coffee shops.
         .WithMany(x => x.Users)         //  Specifies that a Coffee shop can have many Users.       
         .UsingEntity(j => j.ToTable("UserCoffeeShops")); //  Explicitly names the join table. (If I don’t specify this, EF Core will)
 }

Inside CoffeeShopEntity:
    public ICollection<UserEntity> Users { get; } = [];// Navigation property for many-to-many
    public ICollection<UserEntity> Users { get; } = [];// Navigation property for many-to-many

Inside User entity:
        public ICollection<CoffeeShopEntity> CoffeeShops { get; set; } = []; // Navigation property for many-to-many
        public ICollection<CoffeeShopEntity> CoffeeShops { get; set; } = []; // Navigation property for many-to-many


I already have existing users and coffeeshops.
The question is how I suppose to connect between them and add them to the table of UserCoffeeshops?
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ I'm not sure what I'm doing wrong
C#CC# / help
4y ago
❔ I'm trying to implement this networking project, but not sure how to do it?
C#CC# / help
3y ago
Trying to implement SQL database in my app
C#CC# / help
3y ago
❔ I'm not sure what's wrong with my code
C#CC# / help
4y ago