© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
3 replies
Luciferno

How to keep track of third property in Many-To-Many

I have 2 models one that is Reservation and one that is Menu, They have a many-to-many relation but I wanna keep track on the reservation how many times one menu is ordered for that resrevation.

This is my current many-to-many table :
        public int ReservationId { get; set; }
        public Reservation Reservation { get; set; }

        public int MenuId { get; set; }
        public int TimesOrdered { get; set; } <-- this property needs to keep track of how many times a menu has been ordered.
        public Menu Menu { get; set; }
        public int ReservationId { get; set; }
        public Reservation Reservation { get; set; }

        public int MenuId { get; set; }
        public int TimesOrdered { get; set; } <-- this property needs to keep track of how many times a menu has been ordered.
        public Menu Menu { get; set; }

This is my current OnModelCreating for that table
        modelBuilder.Entity<Reservation>()
            .HasMany(x => x.Menus)
            .WithMany(x => x.Reservations)
            .UsingEntity<ReservationMenu>(
                rm => rm.HasOne(x => x.Menu).WithMany().HasForeignKey(k => k.MenuId),
                rm => rm.HasOne(x => x.Reservation).WithMany().HasForeignKey(k => k.ReservationId),
                rm =>
                {
                    rm.HasKey(p => new { p.MenuId, p.ReservationId });
                }
            );
        modelBuilder.Entity<Reservation>()
            .HasMany(x => x.Menus)
            .WithMany(x => x.Reservations)
            .UsingEntity<ReservationMenu>(
                rm => rm.HasOne(x => x.Menu).WithMany().HasForeignKey(k => k.MenuId),
                rm => rm.HasOne(x => x.Reservation).WithMany().HasForeignKey(k => k.ReservationId),
                rm =>
                {
                    rm.HasKey(p => new { p.MenuId, p.ReservationId });
                }
            );

I am kinda stuck on how to approach this.
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

✅ [SignalR] How to effeciently keep track of ConnectionIds.
C#CC# / help
12mo ago
Need to keep track of things in a local application
C#CC# / help
2y ago
✅ How to change of property without setter?
C#CC# / help
3y ago
❔ How to implement an "undo move" function in a game where I need to keep track of multiple objects?
C#CC# / help
3y ago