如何删除"A"实体中的项目以及如何删除"F"链接到"A"实体的"B"实体中的所有其他项目 [英] How to delete an item in 'A' entity and delete all other items in 'B' entity which are linked by FK to 'A' entity

查看:62
本文介绍了如何删除"A"实体中的项目以及如何删除"F"链接到"A"实体的"B"实体中的所有其他项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC为论坛构建应用程序.我有一个名为"Posts"的实体和一个名为"PostReplies"的实体.

I'm using ASP.NET MVC to build an application for Forums. I have an entity named 'Posts' and an entity named 'PostReplies'.

在特定的帖子上,将存在一个由我的"PostReplies"实体中的FK:"Post_Id"链接的回复列表.

On a particular Post, there will be a list of replies which are linked by a FK:'Post_Id' within my 'PostReplies' entity.

我想知道如何删除帖子,然后删除链接到该帖子的回复.

I'm wanting to know how I would go about deleting a Post which would then delete the replies linked to that post.

我已使用Fluent API尝试解决此问题,但出现此错误消息:

I've used Fluent API to try and solve this but I am getting this error message:

在模型生成过程中检测到一个或多个验证错误:

One or more validation errors were detected during model generation:

"BookClub.Data.IdentityUserLogin::EntityType'IdentityUserLogin'没有定义键.为此键定义键. BookClub.Data.IdentityUserRole::EntityType'IdentityUserRole'没有定义?>键.定义此EntityType的键. IdentityUserLogins:EntityType:EntitySet'IdentityUserLogins'基于'IdentityUserLogin'类型,该类型未定义键. IdentityUserRoles:EntityType:EntitySet'IdentityUserRoles'基于未定义键的> IdentityUserRole类型."

"BookClub.Data.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no >key defined. Define the key for this EntityType. BookClub.Data.IdentityUserRole: : EntityType 'IdentityUserRole' has no ?>key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based >on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on >type 'IdentityUserRole' that has no keys defined."

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

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

有人知道如何解决此问题吗?

Does anyone know how to rectify this issue?

帖子实体模型

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }

    public virtual Discussion Discussion { get; set; }
    public virtual ICollection<PostReply> Replies { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

后回复实体模型

public class PostReply
{
    public int Id { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }

    public virtual Post Post { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

删除方法/逻辑

    public void DeletePost(Post post)
    { 

       var deletePost = _context.Post.FirstOrDefault(p => p.Id == id);

        if (deletePost != null)
        {

            _context.Post.Remove(deletePost);
            _context.SaveChanges();
        }

    }

POSTCONTROLLER

POSTCONTROLLER

   [HttpGet]
    public ActionResult DeletePost(int id)
    {

        return View(_postService.GetPost(id));
    }

    [HttpPost]
    public ActionResult DeletePost(Post post)
    {
         var posts = new Post();

            _postService.DeletePost(id, post);


       return RedirectToAction("GetPostsByDiscussion","Discussion", 
         new { id = post.Id })

    }

我已经使用Fluent API并编写了以下代码:

I have used Fluent API and written the following code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PostReply>()
            .HasRequired(p => p.Post)
            .WithMany(p => p.Replies)
            .HasForeignKey(r => r.PostId)
            .WillCascadeOnDelete(false);

    }

推荐答案

您需要在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)

这篇关于如何删除"A"实体中的项目以及如何删除"F"链接到"A"实体的"B"实体中的所有其他项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆