猿问

关系贝特文实体模型的问题

我想在我的项目中制作一个“喜欢”和“不喜欢”系统,我有一个用户模型,一个帖子模型,一个评论模型,其关系如下:


用户 1 ---> * 帖子


用户 1 ---> * 评论


帖子 1 ---> * 评论


现在我想添加一个名为“喜欢”的新模型,其关系如下:


发布 1 ---> * 赞用户 1 ---> * 赞


但是当我想更新数据库时,我收到一个错误,说:“可能导致循环或多个级联路径”,我发现如果我删除我的一个属性,它会修复错误,例如:


public class Post

{


    public Post()

    {


    }


    [Key]

    public int Id { get; set; }


    public string Title { get; set; }


    public virtual List<Like> Likes { get; set; }


    public virtual List<Comment> Comments { get; set; }


}




public class Like

{


    public Like()

    {


    }


    [Key]

    public int Id { get; set; }


    public bool IsLike { get; set; }



    public int postId { get; set; } // I remove these properties


    public virtual Post post { get; set; }



    public int commentId { get; set; }  // I remove these properties


    public virtual Comment comment { get; set; }


}

为了修复“多个级联”错误,我删除了“PostId”和“commentId”属性。


但是当我在数据库中将实体(新数据)添加到我的表(Like)中时,我不知道我的帖子是如何重复的,我的意思是重复的帖子被添加到表中。


任何人都可以告诉我问题吗?


一只萌萌小番薯
浏览 85回答 2
2回答

慕桂英4014372

问题是,您的数据库不够规范化。我看到用户可以创建.他们也可以在 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{&nbsp; &nbsp; &nbsp;public int Id {get; set;}&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp;// Every User creates zero or more Posts (one-to-many)&nbsp; &nbsp; &nbsp;public virtual ICollection<Post> Posts {get; set;}&nbsp; &nbsp; &nbsp;// Every User creates zero or more Comments (one-to-many)&nbsp; &nbsp; &nbsp;public virtual ICollection<Comment> Comments {get; set;}&nbsp; &nbsp; &nbsp;// Every User creates zero or more Likes (one-to-many)&nbsp; &nbsp; &nbsp;public virtual ICollection<Like> Likes {get; set;}}class Post{&nbsp; &nbsp; public int Id {get; set;}&nbsp; &nbsp; ...&nbsp; &nbsp; // Every Post is posted by exactly one User, using foreign key&nbsp; &nbsp; public int PostedByUserId {get; set;}&nbsp; &nbsp; public User User {get; set;}&nbsp; &nbsp; // Every Post has zero or more Comments (one-to-many)&nbsp; &nbsp; public virtual ICollection<Comment> Comments {get; set;}}和类评论和喜欢:class Comment{&nbsp; &nbsp; public int Id {get; set;}&nbsp; &nbsp; ...&nbsp; &nbsp; // Every Comment is posted by exactly one User, using foreign key&nbsp; &nbsp; public int CommentedByUserId {get; set;}&nbsp; &nbsp; public virtual User User {get; set;}&nbsp; &nbsp; // Every Comment is about exactly one Post, using foreign key&nbsp; &nbsp; public int PostId {get; set;}&nbsp; &nbsp; public virtual Post Post {get; set;}&nbsp; &nbsp; // Every Comment has zero or more Likes (one-to-many)&nbsp; &nbsp; public virtual ICollection<Like> Likes {get; set;}}class Like{&nbsp; &nbsp; public int Id {get; set;}&nbsp; &nbsp; ...&nbsp; &nbsp; // Every Like is created by exactly one User, using foreign key&nbsp; &nbsp; public int LikedByUserId {get; set;}&nbsp; &nbsp; public virtual User User {get; set;}&nbsp; &nbsp; // Every Like is about exactly one Comment, using foreign key&nbsp; &nbsp; public int CommentId {get; set;}&nbsp; &nbsp; public virtual Comment Comment {get; set;}}因为我的外键偏离了约定,所以我需要使用 Fluent API 通知实体框架有关这些外键的信息:帖子具有用户的外键:modelBuilder.Entity<Post>()&nbsp; &nbsp; .HasRequired(post => post.User)&nbsp; &nbsp; .WithMany(user => user.Posts)&nbsp; &nbsp; .HasForeignKey(post => post.CreatedByUserId);评论有外键到用户和帖子:var commentEntity = modelBuilder.Entity<Comment>();commentEntity.HasRequired(comment => comment.User)&nbsp; &nbsp; .WithMany(user => user.Comments)&nbsp; &nbsp; .HasForeignKey(comment => comment.CommentedByUserId);commentEntity.HasRequired(comment => comment.Post)&nbsp; &nbsp; .WithMany(post => post.Comments)&nbsp; &nbsp; .HasForeignKey(comment => comment.PostId);Like 有外键到用户和注释:var likeEntity = modelBuilder.Entity<Like>();likeEntity.HasRequired(like => like.User)&nbsp; &nbsp; .WithMany(user => user.Likes)&nbsp; &nbsp; .HasForeignKey(like => like.LikedByUserId);likeEntity.HasRequired(like&nbsp; => like.Comment)&nbsp; &nbsp; .WithMany(comment => comment.Likes)&nbsp; &nbsp; .HasForeignKey(like => like.CommentId);如果将来你想给用户提供喜欢帖子而不是评论的可能性,或者像用户一样,关系将非常相似。首先给用户正确的(每个用户都喜欢零个或多个...),你将自动知道在哪里放置外键virtual ICollection<...>

一只斗牛犬

为了更好的设计,请将 like 表分开,如下所示:PostCommentpublic class User{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; //......&nbsp; &nbsp; public virtual List<Post> Posts { get; set; }&nbsp; &nbsp; public virtual List<Comment> Comments { get; set; }&nbsp; &nbsp; public virtual List<PostLikes> PostLikes { get; set; }&nbsp; &nbsp; public virtual List<CommentLIkes> CommentLikes { get; set; }}public class Post{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; public virtual List<PostLike> PostLikes { get; set; }&nbsp; &nbsp; public virtual List<Comment> Comments { get; set; }}public class Comment{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string CommentBody { get; set; }&nbsp; &nbsp; //.....&nbsp; &nbsp; public virtual List<CommentLike> CommentLikes { get; set; }}public class PostLike{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public int PostId { get; set; }&nbsp; &nbsp; public int UserId {get; set;}&nbsp; &nbsp; public bool IsLike { get; set; }&nbsp; &nbsp; public virtual Post post { get; set; }&nbsp; &nbsp; public virtual User User { get; set; }}public class CommentLike{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public int CommentId { get; set; }&nbsp; &nbsp; public int UserId {get; set;}&nbsp; &nbsp; public bool IsLike { get; set; }&nbsp; &nbsp; public virtual Comment Comment { get; set; }&nbsp; &nbsp; public virtual User User { get; set; }}现在生成一个全新的迁移并相应地更新数据库。注意:在迁移更新时,您可能会遇到级联删除问题。如果您遇到让我知道,我将使用Fluent API配置更新答案。
随时随地看视频慕课网APP
我要回答