简体   繁体   中英

How to use a lookup table in entity?

I have imported my database into entity. All is well except my lookup tables aren't easily accessible anymore.

Example I have a Users table and a Friends table. Many unique users can have the same friends. To handle this I have a Users_Friends table consisting of 2 columns (UserID and FriendID).

I want to be able to query this table against a unique userID to pull all the FriendIDs associated with that particular user. But when I try and do

using (var db = new GameAlertDBEntities())
{
    var friendIDs = db.Users_Friends.
}

I immediately get told cannot resolve symbol Users_Friends. Which I stop there trying to figure out how to resolve the symbol before moving forward. Oddly enough entity finds all my other tables just none of my look up tables.

Many-to-many relationships aux tables are not abstracted into EF tables. Instead you can directly acceess list of related entities like this:

using (var db = new GameAlertDBEntities())
{
    var user = db.Users.First(); // or any other query for user
    var friends = user.Friends;
}

How does this happen? Entity framework recognizes such relationships during import and does all the work behind the scenes.

如果UserId和FriendId未标记为外键,则必须通过简单查询来查找朋友

var userFriends = db.Users.Friends.ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM