2

Consider this Poco:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Fullname { get; set; }
}

Now i want to implement a follow technique where a user may follow other users so basically its self Many to Many relationship

problem is i don't know how exactly i can achieve this in Entity Framework Code-First ?

I thought of a linker Table :

    public class UserFollow
{
    public int Id { get; set; }
    public int Follower { get; set; }
    public int Following { get; set; }
    public DateTime FollowDate { get; set; }
}

i want to be able to get All Followers and Following from every User Object?

Stacker
  • 8,157
  • 18
  • 73
  • 135

1 Answers1

1

This is quite simple using EF code-first as you only need the User POCO:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Fullname { get; set; }

    public ICollection<User> FollowedUsers { get; set; }
}

The collection means that a User is related to other users.

PS: I noted you added a timestamp in your solution example. To achieve that you should still add the collection changing the generic type to whatever suits your needs.

Hope it helps.

Samir Hafez
  • 214
  • 1
  • 7