Cascade.DeleteBehaviour和连接

时间:2018-07-03 19:33:48

标签: c# entity-framework-core

我有以下代码

public class Post
{
    public int Id { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

public class Comment 
{
    public int Id { get; set; }
    public int PostId { get; set; }
    public virtual Post Post { get; set; }
}

public override void Configure(EntityTypeBuilder<Comment> modelBuilder)
{
    modelBuilder.ToTable(nameof(Comment));
    modelBuilder.HasKey(comment => comment.id);

    modelBuilder.HasOne(comment => comment.Post)
                .HasMany(post => post.Comments)
                .HasForeignKey(comment => comment.PostId)
                .OnDelete(DeleteBehaviour.Cascade);

}

因此,当我删除帖子时,与帖子相关的评论也会被删除吗?

0 个答案:

没有答案