Skip to content Skip to sidebar Skip to footer

How Can I Get A List Of Linked 1-n Elements?

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:

Solution 1:

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:

createtable users(
    userid intidentity(1,1) primary key,
    --other columns
    username varchar(10)
)
createtable roles(
    roleid intidentity(1,1) primary key,
    rolename varchar(10)
)
createtable usersroles(
    userid intforeign key references users(userid),
    roleid intforeign key references roles(roleid),
    primary key (userid, roleid)
)

Post a Comment for "How Can I Get A List Of Linked 1-n Elements?"