Search code examples
asp.net-mvcmany-to-manyentity-framework-6

EF 6 - TPC - Many-To-Many Relationship


I'm having trouble with my code-first approach to Entity Framework (6) in a project. I effectively have a database that I am attempting to write code that will cause Entity Framework to replicate. I've gotten close so far, but not 100%. The first issue is many-to-many relationship:

I have a base class called Consumer and it has just basic properties:

public abstract class Consumer
{
    public Guid ID { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime CreateDate { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime? LastModDate { get; set; }
    public int RecordStatus { get; set; }
}

I then want to use inheritance for the subsequent classes:

public class Entity : Consumer
{
    [DisplayName("Entity Name")]
    public string EntityName { get; set; }
    [DisplayName("Phone Number"]
    public string PhoneNumber { get; set; }
    [DisplayName("Doing Business As"]
    public string DBA { get; set; }
}

In my context class, I successfully map all of the properties to the table:

modelBuilder.Entity<Entity>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("Entity");
    });

I continued this design with other classes (contacts for example):

public class Contact : Consumer
{
    [DisplayName("First Name")]
    public string FirstName { get; set; }
    [DisplayName("Last Name"]
    public string LastName { get; set; }
}

Now, obviously, a contact could be related to more than one Entity and an Entity could be related to more than one Contact. How would I code this? Only thing I could think of was to create a related class like so:

public class RelatedContact
{
    public Guid ID { get; set;} 
    public Guid ContactID { get; set; }
    public virtual Contact Contact { get; set; }
    public Consumer Parent { get; set; }
    public virtual Consumer Parent { get; set; }
    public Guid RelationshipTypeID { get; set; }
    public virtual RelationshipType RelationshipType { get; set; }
}

Then after creating the related class, I was assuming I needed to go update my Entity class like so:

public class Entity : Consumer
{
    [DisplayName("Entity Name")]
    public string EntityName { get; set; }
    [DisplayName("Phone Number"]
    public string PhoneNumber { get; set; }
    [DisplayName("Doing Business As"]
    public string DBA { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

Then, I would update my DbContext to map the many relationship, but I don't know the correct syntax or if this is even the correct way to approach this. I am trying to get the following tables to output:

<<Entity>>
ID uniqueidentifier,
CreateDate datetime,
LastModDate datetime,
RecordStatus int,
EntityName varchar(250),
PhoneNumber varchar(100),
DBA varchar(250)

<<Contact>>
ID uniqueidentifier,
CreateDate datetime,
LastModDate datetime,
RecordStatus int,
FirstName varchar(100),
LastName varchar(100)

<<RelatedContact>>
ID uniqueidentifier,
ContactID uniqueidentifier,
ParentID uniqueidentifier,
RelationshipTypeID uniqueidentifier

So, any suggestions? Am I at least headed in the right direction?


Solution

  • To create a many-to-many relationship, you need to use second approach. Just add navigation collection to your Entity and Contact classes. And EF will create linking table for you and track links.

    public class Entity : Consumer
    {
        ... your props
    
        public virtual ICollection<Contact> Contacts { get; set; }
    }
    
    public class Contact : Consumer
    {
        ... your props
    
        public virtual ICollection<Entity> Entities { get; set; }
    }