Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:22:05+00:00 2026-05-28T08:22:05+00:00

I’m trying to create a many-to-many relationship using EF code first in my MVC

  • 0

I’m trying to create a many-to-many relationship using EF code first in my MVC project. I have a scenario where I have a Service which can have many Categories which may have many subcategories. This bit seems to be working fine.

Each Service, Category and Subcategory can also have many HelpDeskMembers associated with it. This is where I’m having trouble creating the association. With the classes below I was expecting EF to create Service_HelpDeskMembers, Category_HelpDeskMembers and Subcategory_HelpDeskMembers tables. Please can somebody guide me in the right direction?

P.S. In the navigation properties, I am not using virtual because I am using AJAX and JSON to create cascading drop down lists between Service/Category/Subcategory and by using ‘virtual’, I was getting a circular reference error.

Here are my classes.

public class Service
{
    [Key]
    public int ServiceID { get; set; }

    public string Title { get; set; }
    public DateTime CreatedDate { get; set; }
    public HelpDeskMember CreatedBy { get; set; }
    public DateTime? DeletedDate { get; set; }
    public HelpDeskMember DeletedBy { get; set; }

    public ICollection<Category> Categories { get; set; }
    public ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}

public class Category
{
    [Key]
    public int CategoryID { get; set; }
    [ForeignKey("Service")]
    public int ServiceID { get; set; }

    public string Title { get; set; }
    public DateTime CreatedDate { get; set; }
    public HelpDeskMember CreatedBy { get; set; }
    public DateTime? DeletedDate { get; set; }
    public HelpDeskMember DeletedBy { get; set; }

    public Service Service { get; set; }
    public ICollection<Subcategory> Subcategories { get; set; }
    public ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}

public class Subcategory
{
    [Key]
    public int SubcategoryID { get; set; }
    [ForeignKey("Category")]
    public int CategoryID { get; set; }

    public string Title { get; set; }
    public DateTime CreatedDate { get; set; }
    public HelpDeskMember CreatedBy { get; set; }
    public DateTime? DeletedDate { get; set; }
    public HelpDeskMember DeletedBy { get; set; }

    public Category Category { get; set; }
    public ICollection<HelpDeskMember> LinesOfSupport { get; set; }       
}

public class HelpDeskMember
{
    public int HelpDeskMemberID { get; set; }
    public string Name { get; set; }
    public bool Admin { get; set; }
    public bool Available { get; set; }
    public DateTime? CreatedDate { get; set; }
    public DateTime? LastLogin { get; set; }
    public DateTime? DeletedDate { get; set; }
    public DateTime? DeletedBy { get; set; }

    public ICollection<Service> Services { get; set; }
    public ICollection<Category> Categories { get; set; }
    public ICollection<Subcategory> Subcategories { get; set; }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:22:06+00:00Added an answer on May 28, 2026 at 8:22 am

    The trick here is to use the model builder to create the many to many link, then it will function as you expect in the db (with a link table)

    Take a look at this article for details on how to do a many to many http://blogs.microsoft.co.il/blogs/gilf/archive/2011/08/01/creating-a-many-to-many-mapping-using-code-first.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.