Entity Framework - Get Followers Posts

I have written a REST API using Entity Framework with Fluent API. In that I want to get the posts created by my followers. I have retrieved the list of followers but want their posts too.

I have User entity and Posts entity as follows:

public class User
{
    public string Id { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }
}

public class BlogPost
{
    public string Summary { get; set; }

    public string Body { get; set; }

    public User CreatedBy { get; set; }
}

And the followers/following relationships between users as follows:

public class UserRelationship
{
    public User UserId { get; set; }

    public User Follower { get; set; }
}

Now how can I retrieve the posts of my followers? Or do I have to change the structure itself?

2 answers

  • answered 2018-04-17 05:26 Slava Utesinov

    You should explicitly specify FKs for using them at query:

    public  class UserRelationship
    {
        [Key, Column(Order = 0)]
        public int UserId { get; set; }
        public User User { get; set; }
    
        [Key, Column(Order = 1)]
        public int FollowerId { get; set; }
        public User Follower { get; set; }
    }
    
    public class BlogPost
    {
        public int Id {get; set;}
        public string Summary { get; set; }
        public string Body { get; set; }
    
        public int CreatedById { get; set; }
        public User CreatedBy { get; set; }
    }
    
    var posts = (from foll in db.UserRelationship
                 where foll.UserId == myId
                 join post in db.BlogPost on foll.FollowerId equals post.CreatedById
                 select post).ToList();
    

  • answered 2018-04-17 05:26 danish

    Retaining your current model structure, you will need to do a get on BlogPost entity for the follower. This will give you the posts from followers. However, this will add additional trip to database and also some translation on the server side code.

    In my opinion, the model should be a bit different. User is the parent entity and it should have details of blog posts too. Thinking in terms of document based NoSQL database, User is easily identifiable as an aggregate so it must be the able to facilitate any traversal your application might need.

    So, in the code, I would prefer something like this:

    public class User
    {
        public int Id
        {
            get; set;
        }
    
        public string Name
        {
            get; set;
        }
    
        public ICollection<BlogPost> Blogs
        {
            get; set;
        }
    
        public ICollection<User> Followers
        {
            get; set;
        }
    }
    
    public class BlogPost
    {
        public int Id
        {
            get; set;
        }
    
        public string Title
        {
            get; set;
        }
    
        public string Content
        {
            get; set;
        }
    
        public User CreatedBy
        {
            get; set;
        }
    }
    

    On the database side, you will need to employ mapping tables approach to set up many to many relationship between users.