0

I have 3 tables, they are very simple. User, Post, Favorite.

            User Table:
            Id: int
            Name: string
            --------------------------------

            Post Table:
            Id: int
            Title: string
            UserId: int  (this is the FK)
            --------------------------------

            Favorite Table:
            UserId: int
            PostId: int
            --------------------------------

I create 2 class files for User and Post tables. Dont know how to add Favorite table there, Favorite table allows User to bookmark their favorite posts.

            public class User : Entity
            {
                public User() { this.Posts = new List<Post>;}
                public virtual string Name { get; set; }
                public virtual IList<Post> Posts {get; set;}
            }

            public class Post : Entity
            {
                public Post() {}
                public virtual string Title { get; set; }
                public virtual User User { get; set; }
            }
qinking126
  • 11,385
  • 25
  • 74
  • 124
  • What you're looking for is probably a many-to-many relation. – alexn Dec 28 '11 at 19:08
  • ye, it is many-to-may relationship. However, from User to Post, it is one-to-many relationship, one User can have many posts, and one Post must belong to one User. Favorite is many-to-many, also between Post and User. I got stuck here, dont know how to continue – qinking126 Dec 28 '11 at 19:13

2 Answers2

0

You have a many to many relation. The Favorite Table is the relation table between User and Post.

public class Favorite : Entity
{
    public virtual Post Post { get; set; }
    public virtual User User { get; set; }
}
Rookian
  • 19,841
  • 28
  • 110
  • 180
  • Rookian, thanks for you help. I added your code in, and modify hte User and post table a little bit, still doesn't look right. – qinking126 Dec 28 '11 at 19:32
0

Rookian, thanks for you help.

After Add your table in, I added public virtual IList Favorites { get; set;} to both User and Post Tables. Still doesn't seem quite right.

            public class User : Entity
            {
                public User() { this.Posts = new List<Post>;}
                public virtual string Name { get; set; }
                public virtual IList<Post> Posts {get; set;}
                public virtual IList<Favorite> Favorites { get; set;}
            }

            public class Post : Entity
            {
                public Post() {}
                public virtual string Title { get; set; }
                public virtual User User { get; set; }
                public virtual IList<Favorite> Favorites { get; set;}
            }

            public class Favorite : Entity
            {
                public virtual Post Post { get; set; }
                public virtual User User { get; set; }
            }
qinking126
  • 11,385
  • 25
  • 74
  • 124
  • I would not add the list of Favortites. I would treat the many to many association as 2 many to one associations. In my opinion this is easier to handle. Have a look at: http://stackoverflow.com/questions/3694632/automapper-bestpractice-of-mapping-a-many-to-many-association-into-a-flat-obje/3704643#3704643 – Rookian Dec 29 '11 at 13:23