I have
User:
-ID
-Name
Badge
-ID
-Name (E.G "User has made 100 posts", the same relationship as Stackoverflow)
So I’ve realised I need a third table, (as badges are defined in the DB aswell so I’ve created:
UserBadge
-ID
-User
-Badge
So now in my User class I have
List<UserBadge> Badges;
Problem is, I’m going
Users.Find(1).Badges.Where(x => x.User == user && x.Badge = badgeIWant)
.FirstOrDefault();
(Or something similar) Because… EF doesn’t have the intelligence (I’m guessing) to automatically know that “Badges” should only include the ones where the User matches.
How should I do this? Was thinking I could have a custom getter for Badges, that has that LINQ conditional in it…
This is EF 4.3 code first:
I’ve tested it and this should work. This would typically be a many to many relationship because User can have many badges and 1 badge can belong to many users. If you run this code, you will find EF creates
BadgeUserstable with BadgeID and UserID as foreign keys. From there on you can query and EF knows which badges you talking about.