2 回答

TA貢獻1871條經驗 獲得超13個贊
問題是,您的數據庫不夠規范化。
我看到用戶可以創建.他們也可以在 a 上發表評論。PostsCommentPostLike
因為 a 是 a,所以 a on this 自動成為 a on 注釋是關于CommentComment about a PostLikeCommentLikePost
換句話說:如果有人創建了關于帖子(10)的評論(4),那么為評論(4)和帖子(20)創建贊是荒謬的。評論(4)與帖子(20)無關!
每個贊都是由一個用戶創建的,恰好是關于一個評論的。用戶創建零個或多個贊(一對多),并且評論已被贊零次或多次(也是一對多)
因此,您有以下一系列操作:
用戶 1 創建帖子 10:帖子 10 具有外鍵 CreateByUserId 1
用戶 2 創建有關帖子 10 的評論 20。Comment 20 已 CommentByUserId 2 和 PostId 20
用戶 3 喜歡評論 20.Like 30 有 LikedByUserId 3 和 CommentId 20
這對于實體框架來說已經足夠規范化了。為了使關系更清晰,我稍微更改了一下外鍵。
class User
{
public int Id {get; set;}
...
// Every User creates zero or more Posts (one-to-many)
public virtual ICollection<Post> Posts {get; set;}
// Every User creates zero or more Comments (one-to-many)
public virtual ICollection<Comment> Comments {get; set;}
// Every User creates zero or more Likes (one-to-many)
public virtual ICollection<Like> Likes {get; set;}
}
class Post
{
public int Id {get; set;}
...
// Every Post is posted by exactly one User, using foreign key
public int PostedByUserId {get; set;}
public User User {get; set;}
// Every Post has zero or more Comments (one-to-many)
public virtual ICollection<Comment> Comments {get; set;}
}
和類評論和喜歡:
class Comment
{
public int Id {get; set;}
...
// Every Comment is posted by exactly one User, using foreign key
public int CommentedByUserId {get; set;}
public virtual User User {get; set;}
// Every Comment is about exactly one Post, using foreign key
public int PostId {get; set;}
public virtual Post Post {get; set;}
// Every Comment has zero or more Likes (one-to-many)
public virtual ICollection<Like> Likes {get; set;}
}
class Like
{
public int Id {get; set;}
...
// Every Like is created by exactly one User, using foreign key
public int LikedByUserId {get; set;}
public virtual User User {get; set;}
// Every Like is about exactly one Comment, using foreign key
public int CommentId {get; set;}
public virtual Comment Comment {get; set;}
}
因為我的外鍵偏離了約定,所以我需要使用 Fluent API 通知實體框架有關這些外鍵的信息:
帖子具有用戶的外鍵:
modelBuilder.Entity<Post>()
.HasRequired(post => post.User)
.WithMany(user => user.Posts)
.HasForeignKey(post => post.CreatedByUserId);
評論有外鍵到用戶和帖子:
var commentEntity = modelBuilder.Entity<Comment>();
commentEntity.HasRequired(comment => comment.User)
.WithMany(user => user.Comments)
.HasForeignKey(comment => comment.CommentedByUserId);
commentEntity.HasRequired(comment => comment.Post)
.WithMany(post => post.Comments)
.HasForeignKey(comment => comment.PostId);
Like 有外鍵到用戶和注釋:
var likeEntity = modelBuilder.Entity<Like>();
likeEntity.HasRequired(like => like.User)
.WithMany(user => user.Likes)
.HasForeignKey(like => like.LikedByUserId);
likeEntity.HasRequired(like => like.Comment)
.WithMany(comment => comment.Likes)
.HasForeignKey(like => like.CommentId);
如果將來你想給用戶提供喜歡帖子而不是評論的可能性,或者像用戶一樣,關系將非常相似。首先給用戶正確的(每個用戶都喜歡零個或多個...),你將自動知道在哪里放置外鍵virtual ICollection<...>

TA貢獻1784條經驗 獲得超2個贊
為了更好的設計,請將 like 表分開,如下所示:PostComment
public class User
{
[Key]
public int Id { get; set; }
//......
public virtual List<Post> Posts { get; set; }
public virtual List<Comment> Comments { get; set; }
public virtual List<PostLikes> PostLikes { get; set; }
public virtual List<CommentLIkes> CommentLikes { get; set; }
}
public class Post
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public virtual List<PostLike> PostLikes { get; set; }
public virtual List<Comment> Comments { get; set; }
}
public class Comment
{
[Key]
public int Id { get; set; }
public string CommentBody { get; set; }
//.....
public virtual List<CommentLike> CommentLikes { get; set; }
}
public class PostLike
{
[Key]
public int Id { get; set; }
public int PostId { get; set; }
public int UserId {get; set;}
public bool IsLike { get; set; }
public virtual Post post { get; set; }
public virtual User User { get; set; }
}
public class CommentLike
{
[Key]
public int Id { get; set; }
public int CommentId { get; set; }
public int UserId {get; set;}
public bool IsLike { get; set; }
public virtual Comment Comment { get; set; }
public virtual User User { get; set; }
}
現在生成一個全新的遷移并相應地更新數據庫。
注意:在遷移更新時,您可能會遇到級聯刪除問題。如果您遇到讓我知道,我將使用Fluent API配置更新答案。
- 2 回答
- 0 關注
- 124 瀏覽
添加回答
舉報