0

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?

clockwiseq
  • 4,189
  • 9
  • 38
  • 61

1 Answers1

1

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; }
}
Pavel
  • 526
  • 2
  • 5
  • So the goal is to have a single RelatedContact table. I have other class objects that will have related Contacts as well and wanted to share the table and use the base class Consumer as the "Parent" portion of the foreign key. Is that possible? – clockwiseq Nov 05 '14 at 20:23
  • Answering your question "is to possible?" - yes, it is. But what benifit you will get from this approach? It will not create a big impact to database if you add additional table for each relation. But all your action will be managed by EF. And this will save a lot of time of developement for you. – Pavel Nov 05 '14 at 20:31
  • The reason I need a single table is that I am attempting to mimic an existing database and create a proof-of-concept that can demonstrate that with code and the right models, EF can create the same tables and relationships. I believe I found my solution by connecting to our existing database, then create a EF Data Model for those tables to see what VS2013 would come up with and I might add my own answer once I get it cleaned up. Thank you for the response. – clockwiseq Nov 05 '14 at 20:38
  • [Check this post](http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table) about having linked table – Pavel Nov 05 '14 at 20:39
  • Also there is a goog article how to generate EF codefirst model http://msdn.microsoft.com/en-us/data/jj200620.aspx – Pavel Nov 05 '14 at 20:58