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 关注
- 84 浏览
添加回答
举报