关系beetwen实体模型的问题

时间:2019-02-11 15:53:56

标签: c# asp.net-mvc entity-framework-6 ef-code-first

我想在我的项目中创建一个Like and Dislike系统, 我有一个用于User的模型,一个用于Post的模型,一个具有以下关系的Comment的模型:

用户1 ---> *发布

用户1 ---> *评论

帖子1 ---> *评论

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

帖子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”属性。

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

有人可以告诉我这个问题吗?

2 个答案:

答案 0 :(得分:0)

为获得更好的设计,请按如下所示将int result = job.waitForCompletion(true) ? 0 : 1; if (result == 0) { // status code OK // ls job output directory, collect part-r-XXXXX file names // create HDFS readers for files // merge them in a single file in whatever way you want } Post的like表分开:

Comment

现在生成一个全新的迁移并相应地更新数据库。

注意:您可能会在迁移更新时遇到级联删除问题。如果您遇到困难,请告诉我,我将使用Fluent API配置更新答案。

答案 1 :(得分:0)

问题是您的数据库标准化不够。

我看到用户可以创建>>> list(map(str.strip, _.split(","))) ['green_main', 'yellow_main', 'red_main', 'green_first', 'yellow_first', 'red_first', 'clk', 'rst', 'waiting_main', 'waiting_first'] 。他们还可以在PostsComment,并且可以Post条评论。

由于LikeComment,因此此Comment about a Post上的Like会自动成为Comment上的Like关于

换句话说:如果某人创建了关于帖子(10)的评论(4),那么为评论(4)和帖子(20)创建一个赞是荒谬的。评论(4)与帖子(20)无关!

“每个人都喜欢”是由一个用户创建的,仅包含一个评论。用户创建了零个或多个“点赞”(一对多),并且评论被点赞了零次或多次(也是一对多)

因此,您需要执行以下操作顺序:

  • 用户1创建帖子10:帖子10具有外键CreateByUserId 1
  • 用户2创建有关帖子10的评论20。评论20具有CommentedByUserId 2和PostId 20
  • 用户3喜欢评论20。喜欢30的用户喜欢LikedByUserId 3和CommentId 20

对于实体框架,这已经足够标准化。为了使关系更加清晰,我对外键做了一些更改。

Post

和类注释和赞:

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;}
}

由于我的外键与惯例有所不同,因此我需要使用流利的API将这些外键告知实体框架:

帖子具有用户的外键:

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;}
}

注释具有指向用户和帖子的外键:

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);

如果将来您想让用户有可能喜欢帖子而不是评论,或者可能喜欢用户,则关系将非常相似。首先给用户适当的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); (每个用户都喜欢零或更多...),您会自动知道将外键放在哪里