Reputation: 751
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.
Upvotes: 1
Views: 2761
Reputation: 34238
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
Upvotes: 3