That's my basic (Database First
) diagram on SQL Server
:
When I Update Model from Database
using Entity Framework
(6.x) within my MVC
application, I expect Users
got this property:
public virtual ICollection<Role> Roles { get; set; }
As well Roles with:
public virtual ICollection<User> Users { get; set; }
But instead I got:
public virtual ICollection<UsersRoles> UsersRoles { get; set; }
public virtual ICollection<UsersRoles> UsersRoles { get; set; }
Where am I wrong?
Your design is wrong.
Many-to-many relation is defined by a table, which consist only of two IDs of considerated tables, which togeteher form composite key. You have almost this design, but you have additional primary key to "relation" table, which ruins everything :)
If you use this code to generate your tables, you'll get correct relations: