I have the following Schema:
User Table: (Primary Key)
- UserId
- CustomerId
Role Table: (Primary Key)
- UserId
- CustomerId
UserRole Table:
- UserRoleId (UNIQUEIDENTIFIER (newsequentialid)) Primary Key
- UserId
- Customerid
- 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?