asp.net mvc - MVC EF get many-to-many relationship table -
i set many-to-many relationship in ef. can refer main tables, not bridge table. how can refer bridge table?
for example:
user: userid, name group: groupid, group usergroup: userid, groupid
i can refer user table , group table typing
context.user.(some linq expression) context.group.(some lin1 expression)
but cannot refer bridge table typing
context.usergroup
it great know versions of mvc , ef working with. based on assumption mvc 4 , ef5...
the point of join table keep referential integrity going. @timothy says, there no reason refer (usergroup) join table unless there actual additional data in i.e. payload. if you're putting other data in usergroup table not join table anyway.
rather use lazy or eager loading group data need:
lazy loading
var users = context.users foreach (user x in users) { foreach (group f in x.groups) //notice x user being referred here groups { // whatever want objects } }
eager loading
var users = context.users.include(g => g.groups) foreach (user x in users) { foreach (group f in x.groups) //notice x user being referred here groups { // whatever want objects } }
the 'foreach' code above added show how include in loop example. first line of these loading examples important.
once have defined 'var users' rest "easy". luck!
Comments
Post a Comment