0

I'm working with EF4.1 Code First and am trying to create some Many-to-Many relationship tables where there tables would need to be linked. Please see small snippet of code beloow:

class Event
{
    int EventId { get; set; }
    ICollection<Contact> Contacts { get; set; }
}

class Contact
{
    int ContactId { get; set; }
    ICollection<Relation> Relations { get; set; }
}

class Relation
{
    int RelationId { get; set; }
    string Name { get; set; }
}   

So the Contacts object can have many different types of Relationship, such as "Mother", "Father", "Brother", etc.

I need to track some kind of Event where a Contact attended, but I want to know how he is related to the person hosting the Event. So for example, was he the Eventer's Brother, Father or Husband? At another event, the same person could show up, but be the Eventer's Brother-in-Law.

Event to Contact is Many-to-Many; Relation to Contact is One-to-Many.

In SQL, we would just made a link table and have all three properties Id's there (EventId, ContactId, RelationId); however, in Code First, how would you represent this relationship?

Tomasz Iniewicz
  • 4,379
  • 6
  • 42
  • 47

1 Answers1

4

Same as database you have to create a mapping entity just like ContactEvents like mapping table in database.

class Event
{
  int EventId { get; set; }
  ICollection<ContactEvent> ContactEvents { get; set; }
}

class ContactEvent
{
  int EventId {get;set;}
  int ContactId {get;set;}
  public virtual Event Event {get; set;}
  public virtual Contact Contact {get;set;}
}

class Contact
{
   int ContactId { get; set; }
   ICollection<ContactEvent> ContactEvents { get; set; }
   ICollection<Relation> Relations { get; set; }
}

class Relation
{
  int RelationId { get; set; }
  string Name { get; set; }
  public virtual Contact Contact {get; set}
}   
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • hmm... although this was my first thought, in Code First, you can wire up a many-to-many relationship without creating a mapping object, so I'm trying to see if I the same thing can be done without creating a whole new object. – Tomasz Iniewicz May 21 '12 at 18:44
  • I don't know why would you avoid to create a whole new object? any reasons? – Asif Mushtaq May 21 '12 at 18:45
  • also, this doesn't exactly answer the question because in your code, you point out how to link Contact to Event, but I need to link Contact, Event AND Relation. A many-to-many relationship can be configured using conventions. I'm essentially looking for a many-to-many-to-many relationship. – Tomasz Iniewicz May 21 '12 at 18:47
  • I wouldn't mind creating another object, but if there is a better way to do it, why do not do it that way? – Tomasz Iniewicz May 21 '12 at 18:48
  • To track entities you will need virtual properties, add virtual properties and you can track the Contact, Event and Relations, I think you need to create an entity for every table in your database. – Asif Mushtaq May 21 '12 at 18:51
  • from my research of the EntityFramework Code First and actually building a functioning application, this is not the case. As I stated above, pure convention will give you what you need to have EF building out something like a Many-to-Many relationship table that is not represented as a POCO class. – Tomasz Iniewicz May 21 '12 at 18:55
  • it looks like you are correct. I just created the sample database with the linking table in SQL Server and then ran a [Reverse Engineer](http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d) using the EF PowerTools and it came out with similar, only you forgot to add the Relation navigation property. I was hoping for something more elegant, but not sure if I find any. Perhaps someone will have a better way. I will wait with marking this as the answer, but surely you deserve credit. Thanks. – Tomasz Iniewicz May 21 '12 at 20:05