Entity Framework: execute a query in a link table / between many relationships

I am trying to execute a query using linq for entities so that the entity / table does not contain the same values ​​before updating it.

The structure of the database is as follows:

Users             User_IPAddresses          IPAddresses
-----             ----------------          -----------
UserID   >------  UserID           ------<  IPAddressID
User              IPAddressID               Address

So, the structure of the entity object is as follows

UserSet          IPAddressSet
-------  >-----< ------------
User             IPAddress

All identifier fields are primary keys, so the link table (User_IPAddresses) must contain unique rows.

The problem I am facing is that I cannot figure out how to inspect objects so that I do not violate the restriction of a unique row in the User_IPAddresses table before updating it.

Any EF gurus who can help me?

+3
1
//returns true if pair exists
public bool CheckIfUserIPPairExists(int ipID, int userID)
{

   bool exists 
      = db.UserSet.Any(user=>user.UserID==userID 
                             && user.IPAddress.Any(ip=>ip.IPAddressID == ipID));
   return exists;
}
+4

Source: https://habr.com/ru/post/1711738/


All Articles