Many-to-many relationship between same table with extra column in EF

Nico

LOL. This is perhaps the most complicated case for EF to do the modeling? I even doubt if this scenario is supported. My business is like I need to know which Application matches another Application for what reason. So the table representing the relationship looks like this:

PrimaryApplicationID, SecondaryApplicationID, MatchedRuleID

OK. Now is it even possible I can use EF to represent this relationship? If yes, what special thing I need to do in database side? I'm using database-first approach, but this doesn't quite matter, does it?

Thanks for any clue.

Slauma

With Database-First you would create three tables, for example:

  • Table Applications

    ApplicationId           int  NOT NULL PK
    ...Other columns...
    
  • Table Rules

    RuleId                  int  NOT NULL PK
    ...Other columns...
    
  • Table MatchingApplications

    PrimaryApplicationId    int  NOT NULL PK, FK to Applications table
    SecondaryApplicationId  int  NOT NULL PK, FK to Applications table
    MatchedRuleId           int  NOT NULL FK to Rules table
    

The double-PK in MatchingApplications is meant to be a composite key. The double-FK is meant as two separate foreign key relationships to the same table Applications. You cannot enable cascading delete for both relationships because SQL Server won't allow more than one cascading delete path between the same tables.

EF should create three entities from this database schema:

  • Application entity with two collections with element type MatchingApplication
  • Rule entity with one collection with element type MatchingApplication
  • MatchingApplication entity with two navigation references of type Application and one navigation reference of type Rule.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sequelize many-to-many relationship table gets unneeded extra column

From Dev

Many to many relationship in the same table?

From Dev

Many-to-Many Relationship in Hibernate with Extra Column

From Dev

Hibernate query on many to many relationship with extra column

From Dev

Many-to-Many Relationship in Hibernate with Extra Column

From Dev

Many-to-many relationship between the same class

From Dev

One to many relationship on the same table

From Dev

Many-to-Many Relationships to Same Table with EF

From Dev

Defining a many-to-many relationship referencing the same table (EF7/core)

From Dev

JPA Many-To-Many relationship with an extra enum column

From Dev

SQLAlchemy - Self referential Many-to-many relationship with extra column

From Dev

Many-to-many relationship with composite keys, extra column in Hibernate

From Dev

Add additional column to existing many to many relationship - EF Code First

From Dev

Add additional column to existing many to many relationship - EF Code First

From Dev

EF Code first - many to many relation mapping table with extra columns

From Dev

EF delete many to many relationship

From Dev

Build bidimensional table based on many-to-many EF relationship

From Dev

How express the many to many relationship with single parent table in EF 5

From Dev

EF6 Many to Many relationship, multiple tables to join table

From Dev

Fetching one to many relationship from same table

From Dev

Multiple where clauses on the same column with a many to many relationship

From Dev

Multiple where clauses on the same column with a many to many relationship

From Dev

how to query many-to-many relationship in same table by redbeanphp?

From Dev

EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

From Dev

merge in jpa many-to-many with extra column in join table

From Dev

merge in jpa many-to-many with extra column in join table

From Dev

Update a column that exist in the many to many relationship table in laravel 5.2

From Dev

Recursive relationship on a many to many table

From Dev

SQL many to many relationship table

Related Related

  1. 1

    Sequelize many-to-many relationship table gets unneeded extra column

  2. 2

    Many to many relationship in the same table?

  3. 3

    Many-to-Many Relationship in Hibernate with Extra Column

  4. 4

    Hibernate query on many to many relationship with extra column

  5. 5

    Many-to-Many Relationship in Hibernate with Extra Column

  6. 6

    Many-to-many relationship between the same class

  7. 7

    One to many relationship on the same table

  8. 8

    Many-to-Many Relationships to Same Table with EF

  9. 9

    Defining a many-to-many relationship referencing the same table (EF7/core)

  10. 10

    JPA Many-To-Many relationship with an extra enum column

  11. 11

    SQLAlchemy - Self referential Many-to-many relationship with extra column

  12. 12

    Many-to-many relationship with composite keys, extra column in Hibernate

  13. 13

    Add additional column to existing many to many relationship - EF Code First

  14. 14

    Add additional column to existing many to many relationship - EF Code First

  15. 15

    EF Code first - many to many relation mapping table with extra columns

  16. 16

    EF delete many to many relationship

  17. 17

    Build bidimensional table based on many-to-many EF relationship

  18. 18

    How express the many to many relationship with single parent table in EF 5

  19. 19

    EF6 Many to Many relationship, multiple tables to join table

  20. 20

    Fetching one to many relationship from same table

  21. 21

    Multiple where clauses on the same column with a many to many relationship

  22. 22

    Multiple where clauses on the same column with a many to many relationship

  23. 23

    how to query many-to-many relationship in same table by redbeanphp?

  24. 24

    EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

  25. 25

    merge in jpa many-to-many with extra column in join table

  26. 26

    merge in jpa many-to-many with extra column in join table

  27. 27

    Update a column that exist in the many to many relationship table in laravel 5.2

  28. 28

    Recursive relationship on a many to many table

  29. 29

    SQL many to many relationship table

HotTag

Archive