Entity Framework Code First "join table"

c# ef-code-first ef-migrations entity-framework entity-framework-6

Question

Entity Framework Code First is the platform I'm using.

I have a thing I call Notes.

Additional Entities that I have include BusinessPartners, Opportunities, and WorkOrders.

These things might all have notes.

What model would work best for this?

1.) The business partners, opportunities, and workorders tables in the notes table all contain optional foreign keys. then simply set the related optional key for the note.

2.) Use intermediary tables with fields like BusinessPartnerNotes, which include BusinessPartnerId and NoteId

A note will never be associated to two entities at the same time, it should be noted.

Any advice or assistance would be greatly appreciated.

1
0
1/9/2016 9:15:36 PM

Accepted Answer

I would choose the most straightforward strategy in light of your description of the cardinalities and assuming that Notes for BusinessPartners have the same format as Notes for Opportunities (option 1. in your list).

class Note
{
    public int Id { get; set; }
    public string Content { get; set; }
}
class BusinessPartner
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Note> Notes { get; set; }
}
class Opportunity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Note> Notes { get; set; }
}

which ought to produce the tables below:

Notes
  Id
  Content
  BusinessPartner_Id
  Opportunity_Id
BusinessPartners
  Id
  Name
Opportunities
  Id
  Name
1
1/9/2016 9:57:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow