Does a many-many relationship in EF6 require the mapping table to be defined and to have a primary key?

user1679941

Does an EF6 table used for many to many require a primary key and do I have to create a class for that many to many table? I have seen two ways of creating a many-many table now. The one that's been answered here in this question:

How do I name a many-many table for EF6 and do I need to add special mapping for this?

and the one here in this question:

Entity Framework : many to many relationship , Insert and update

The first answer suggests a table with a primary key while the second answer (with the 6 votes) suggests a very different approach where the many-many table is not defined in EF?

My thoughts right now are that a primary key is not needed. The reason I am thinking this is because when I look at the new tables created by the ASP.NET Identity then the UserRoles table that is many to many does not have a primary key.

Colin

When two entities are related to each other with a many-to-many relationship Entity Framework Code First will create a join table without you having to create an entity class to model the join.

The join table does have a primary key but in this case it is a composite made up from two foreign keys to the tables supporting your entities. In your first link I think the answer is misleading because EF would not generate that sql from that fluent api - but it does tell you how to name the columns and table if you don't like the defaults.

The second example is using database first and modelling the many-to-many in the same standard way. i.e. a primary key that is a composite of two foreign keys.

What you should be aware of is that sometimes you start with a many-to-many relationship, then you realise that the relationship itself has some attributes - and then you have to promote the relationship to an entity in its own right and you will probably add a different primary key

Reference:

Configuring a Many-to-Many Relationship

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I have a one to many relationship between a primary key in one table and a primary key in a second table?

From Dev

EF 6 Primary key violation with Many To Many relationship

From Dev

EF6 Many to Many relationship, multiple tables to join table

From Dev

The table does not have a primary key defined error

From Dev

Entity Framework Many TO Many Relationship with Primary Key

From Dev

Mapping the other table in a many to many relationship

From Dev

Primary key in "many-to-many" table

From Dev

JPA/Hibernate: Map many-to-many relationship when join table has own primary key

From Dev

Flask-Sqlalchemy, Primary key for secondary table in many-to-many relationship

From Dev

why doesn't Hibernate generate primary key for many-to-many relationship table?

From Dev

Flask-Sqlalchemy, Primary key for secondary table in many-to-many relationship

From Dev

Mapping One to Many on non-primary key

From Dev

Mapping One to Many on non-primary key

From Dev

Error 6002: The table/view does not have a primary key defined

From Dev

EntityFramework throwing Error : The table does not have a primary key defined

From Dev

Deleting an entity record in a many to many relationship with EF6

From Dev

Manage EF6 database first many to many relationship

From Dev

Deleting an entity record in a many to many relationship with EF6

From Dev

EF6 Child entities not updating in many-to-many relationship

From Dev

save many-to-many relationship in corresponding mapping-table

From Dev

Does a user to group (many-to-many) relational table in MySQL needs PRIMARY KEY?

From Dev

One-to-Many relation to existed table in EF6: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

From Dev

Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

From Dev

Hibernate - What primary key should I use in a one to many relationship without an ID on the child table

From Dev

EntityFramework 6 relationship mapping to many items

From Dev

Reference primary key from many-to-many table

From Dev

Does not have a primary key defined and no valid primary key could be inferred

From Dev

Mapping Parent and Children relationship collections to a single table in EF6

From Dev

Hibernate: Mapping with one-to-many on non-primary-key columns

Related Related

  1. 1

    Can I have a one to many relationship between a primary key in one table and a primary key in a second table?

  2. 2

    EF 6 Primary key violation with Many To Many relationship

  3. 3

    EF6 Many to Many relationship, multiple tables to join table

  4. 4

    The table does not have a primary key defined error

  5. 5

    Entity Framework Many TO Many Relationship with Primary Key

  6. 6

    Mapping the other table in a many to many relationship

  7. 7

    Primary key in "many-to-many" table

  8. 8

    JPA/Hibernate: Map many-to-many relationship when join table has own primary key

  9. 9

    Flask-Sqlalchemy, Primary key for secondary table in many-to-many relationship

  10. 10

    why doesn't Hibernate generate primary key for many-to-many relationship table?

  11. 11

    Flask-Sqlalchemy, Primary key for secondary table in many-to-many relationship

  12. 12

    Mapping One to Many on non-primary key

  13. 13

    Mapping One to Many on non-primary key

  14. 14

    Error 6002: The table/view does not have a primary key defined

  15. 15

    EntityFramework throwing Error : The table does not have a primary key defined

  16. 16

    Deleting an entity record in a many to many relationship with EF6

  17. 17

    Manage EF6 database first many to many relationship

  18. 18

    Deleting an entity record in a many to many relationship with EF6

  19. 19

    EF6 Child entities not updating in many-to-many relationship

  20. 20

    save many-to-many relationship in corresponding mapping-table

  21. 21

    Does a user to group (many-to-many) relational table in MySQL needs PRIMARY KEY?

  22. 22

    One-to-Many relation to existed table in EF6: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

  23. 23

    Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

  24. 24

    Hibernate - What primary key should I use in a one to many relationship without an ID on the child table

  25. 25

    EntityFramework 6 relationship mapping to many items

  26. 26

    Reference primary key from many-to-many table

  27. 27

    Does not have a primary key defined and no valid primary key could be inferred

  28. 28

    Mapping Parent and Children relationship collections to a single table in EF6

  29. 29

    Hibernate: Mapping with one-to-many on non-primary-key columns

HotTag

Archive