none
handling Many to Many RelationShip in EF RRS feed

  • Question

  • Scenario is,

    I have Two Table in Database (Class and Group) they have many to many relationship so a table that contains both of the table's PK as FK as below

    ClassGroupTable (Class_PK,GroupPK)

    CRUD for this relation is handled by EF automatically, thats fine

    but what if? I want to have an other many to many relationship between ClassGroupTable and Subjects Table?

    as I told ClassGroupTable is Junction table for Class and Group Table so now how EF will handle relationship (M-to-M) between Junction table and another table(Subjects)...

    please tell me clear concept so that I can proceed with my project

    Saturday, January 16, 2016 10:51 AM

Answers

All replies

  • In that case ClassGroup will become an Entity, and will appear in your model like any other table. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Saturday, January 16, 2016 2:30 PM
  • then how i will handle PK? of ClassGroup as its composite PK
    Sunday, January 17, 2016 8:26 AM
  • EF understands composite keys. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Sunday, January 17, 2016 2:19 PM
  • and EF will  do entrires in ClassGroup also as before it was doing when it was not in model as due to M to M relationSHip?
    Sunday, January 17, 2016 4:25 PM
  • >and EF will  do entrires in ClassGroup also as before it was doing when it was not in model as due to M to M relationship?

    Not quite.  You'll have to handle the ClassGroup items in code.  But you can write convenience methods on Class and Group to traverse across ClassGroup. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Sunday, January 17, 2016 5:09 PM
  • David,

    ClassGroup will have M to M relationship with Subject Table, So a New table will be made

    ClassGroupSubject :(ClassId,GroupId,SubjectId)

    in this table ClassId and GroupId will reference to ClassGroup table or will reference to Class and Group tables? Clear me this tooo..... as classId groupid will be composite FK? in ClassGroupSubject? and Suject Id will be Single FK that will reference to Subject Table

    Monday, January 18, 2016 1:43 PM
  • >in this table ClassId and GroupId will reference to ClassGroup table

    I believe so.

    >or will reference to Class and Group tables

    No, that would be a different model.

    >as classId groupid will be composite FK? in ClassGroupSubject? and Suject Id will be Single FK that will reference to Subject Table

    I believe so, but you should just test this out for yourself.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, January 18, 2016 5:03 PM