亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關系貝特文實體模型的問題

關系貝特文實體模型的問題

C#
一只萌萌小番薯 2022-09-04 16:38:25
我想在我的項目中制作一個“喜歡”和“不喜歡”系統,我有一個用戶模型,一個帖子模型,一個評論模型,其關系如下:用戶 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)中時,我不知道我的帖子是如何重復的,我的意思是重復的帖子被添加到表中。任何人都可以告訴我問題嗎?
查看完整描述

2 回答

?
慕桂英4014372

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<...>


查看完整回答
反對 回復 2022-09-04
?
一只斗牛犬

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配置更新答案。


查看完整回答
反對 回復 2022-09-04
  • 2 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號