Linking entities from two tables using a Many-to-Many... why so hard!? RRS feed

  • Question

  • I have three tables in my database where one of the tables is a Many-to-Many with foreign keys to the primary keys of my two tables.  We'll assume I'm talking about teachers and students since that seems to be a common theme.

    ID (PK)-----------------TeacherFK-------------------------ID (PK)

    In my code I can add a Teacher with no problems... I can also add a Student.  Where I run into problems is when I want to associate the two of them.  Back in the day I'd just drop the PK of both the Teacher and Student in their respective column... but EF doesn't make it that easy anymore (or I'm suffering from a case of Derpatitus).  If I have an ID then how do I map it to my other entity?

    I feel like this is the one thing I don't understand at all about EF.  Hopefully this answer will work with other things like cascading deletes or updates.

    Thanks in advance.  ^_^

    Monday, February 13, 2012 3:02 AM


All replies