Entity Framework Many TO Many Relationship with Primary Key

Sam

I have the following Schema:

User Table: (Primary Key)

  1. UserId
  2. CustomerId

Role Table: (Primary Key)

  1. UserId
  2. CustomerId

UserRole Table:

  1. UserRoleId (UNIQUEIDENTIFIER (newsequentialid)) Primary Key
  2. UserId
  3. Customerid
  4. RoleId

Those tables participate in many to many relationship (UserRole). I am using Entity Framework code first with mapping classes to define the database tables. So, In my mapping class for User Table, I have the following:

 this.HasMany(u => u.Roles)
      .WithMany()
      .Map(m =>
        {
          m.MapLeftKey(new string[] { "CustomerID", "UserID" });
          m.MapRightKey(new string[] {"CustomerID", "RoleID"});
          m.ToTable("UserRoles");
        }
      );

Entity framework is failing with this message: "One or more validation errors were detected during model generation: CustomerID: Name: Each property name in a type must be unique. Property name 'CustomerID' is already defined. UserRole: EntityType: EntitySet 'UserRole' is based on type 'UserRole' that has no keys defined.

is it possible to tell Code First that the Primary Key for my "UserRole" is UserRoleId?

The issue is when Entity Framework tries to create the UserRole Table, it would use all columns of MapLeftKey and MapRightKey to Create UserRole with PrimaryKey that has all those columns.

Any suggestions?

T McKeown

You need to model your classes similar to your DB, why don't you simply add the association tables? I mocked up your DB and there is no problem as long as you model all the tables.

Test it for yourself, create an EF project .edmx using code first from existing DB, I think the answer will be obvious.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework - many to many with primary key and composite foreign key

From Dev

Many to many relationship in Entity Framework

From Dev

Entity Framework: One to Many relationship

From Dev

Entity Framework many to many relationship Delete

From Dev

Saving Many to Many relationship - Entity Framework

From Dev

Entity Framework specify cardinality in a many to many relationship

From Dev

Entity framework many-to-many relationship

From Dev

How many to many relationship in Entity Framework is working

From Dev

Update a Many to Many relationship using Entity Framework?

From Dev

Entity Framework duplicate entries in many to many relationship

From Dev

Updating many-to-many relationship entity framework

From Dev

Entity Framework with many to many relationship generetad tables

From Dev

Entity Framework Core Creating a Many to Many Relationship

From Dev

primary key constraint updating many-to-many self referential table in entity framework code-first

From Dev

Entity Framework one-to-many and many-to-many relationship

From Dev

Entity Framework one-to-many and many-to-many relationship

From Dev

Eager Loading Many to Many relationship with an Association Entity - Entity Framework

From Dev

EF 6 Primary key violation with Many To Many relationship

From Dev

Entity Framework 4.2 One to many relationship

From Dev

One to many relationship in Entity Framework 6

From Dev

Entity Framework One-to-Many relationship with ordering

From Dev

Entity Framework - Trouble with one-to-many relationship

From Dev

entity framework 5, delete one to many relationship?

From Dev

EF core many to many relation violation of primary key constraint entity

From Dev

Exception with Entity Framework 6, disconnected context and many-to-many relationship

From Dev

How to use Entity Framework 6 to update many-to-many relationship?

From Dev

Keep list of foreign keys in many-to-many Entity Framework relationship

From Dev

Mapping many to many relationship in entity framework code first

From Dev

Entity Framework not writing many to many relationship changes to the database

Related Related

  1. 1

    Entity Framework - many to many with primary key and composite foreign key

  2. 2

    Many to many relationship in Entity Framework

  3. 3

    Entity Framework: One to Many relationship

  4. 4

    Entity Framework many to many relationship Delete

  5. 5

    Saving Many to Many relationship - Entity Framework

  6. 6

    Entity Framework specify cardinality in a many to many relationship

  7. 7

    Entity framework many-to-many relationship

  8. 8

    How many to many relationship in Entity Framework is working

  9. 9

    Update a Many to Many relationship using Entity Framework?

  10. 10

    Entity Framework duplicate entries in many to many relationship

  11. 11

    Updating many-to-many relationship entity framework

  12. 12

    Entity Framework with many to many relationship generetad tables

  13. 13

    Entity Framework Core Creating a Many to Many Relationship

  14. 14

    primary key constraint updating many-to-many self referential table in entity framework code-first

  15. 15

    Entity Framework one-to-many and many-to-many relationship

  16. 16

    Entity Framework one-to-many and many-to-many relationship

  17. 17

    Eager Loading Many to Many relationship with an Association Entity - Entity Framework

  18. 18

    EF 6 Primary key violation with Many To Many relationship

  19. 19

    Entity Framework 4.2 One to many relationship

  20. 20

    One to many relationship in Entity Framework 6

  21. 21

    Entity Framework One-to-Many relationship with ordering

  22. 22

    Entity Framework - Trouble with one-to-many relationship

  23. 23

    entity framework 5, delete one to many relationship?

  24. 24

    EF core many to many relation violation of primary key constraint entity

  25. 25

    Exception with Entity Framework 6, disconnected context and many-to-many relationship

  26. 26

    How to use Entity Framework 6 to update many-to-many relationship?

  27. 27

    Keep list of foreign keys in many-to-many Entity Framework relationship

  28. 28

    Mapping many to many relationship in entity framework code first

  29. 29

    Entity Framework not writing many to many relationship changes to the database

HotTag

Archive