且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除"A"实体中的项目以及如何删除"F"链接到"A"实体的"B"实体中的所有其他项目

更新时间:2023-02-14 20:07:20

您需要在DBContext

这可能是一对多,一对一,多对多

您可以在此处

我使用的是MVC的默认ApplicationDbContext,因此数据库中有ApplicationUser的表.

I'm using MVC's default ApplicationDbContext and therefore have ApplicationUser's tables in my database.

如果要继承ApplicationDbContext,则应在模型创建方法中调用base.OnModelCreating()方法.

if you are inheriting the ApplicationDbContext you should call the base.OnModelCreating() method in your model creating method.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<PostReply>()
        .HasRequired(p => p.Post)
        .WithMany(p => p.Replies)
        .HasForeignKey(r => r.PostId)
        .WillCascadeOnDelete(true);
}

要启用级联删除,您应该发送.WillCascadeOnDelete(true)

And to enable cascade delete, you should send true parameter like .WillCascadeOnDelete(true)