Using Entity Framework, I am writing a social networking app and trying to setup some relationships so that I can have Users with Followers/Following properties.
I have a Users table:
Users
_______
Id
FirstName
LastName
Email
Then I have a Follows table:
Follows
__________
FollowerId
FolloweeId
On a strongly typed User object, I want to see .Followers and .Following properties that return collections of User objects. So far I’ve tried making the columns in the Follows table as foreign keys, and both as composite primary keys. The entity model comes out the way I want it (I don’t have an Entity in-between, and I just have to rename the navigation properties), but when I populate with data, the Followers and Following collections are empty, so something is not right with the relationships.
I suppose I could separate and have two tables, Followers and Following, but I would have duplicate data and would have to add to two tables when someone follows someone else.
You need to include the data for the tables you User tables has relationships with in your query.
A query like this:
Will let you have access to the objects for those included tables.