How to delete an item in 'A' entity and delete all other items in 'B' entity which are linked by FK to 'A' entity

坚强是说给别人听的谎言 提交于 2020-05-17 06:45:10

问题


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

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.

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

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

Does anyone know how to rectify this issue?

POST ENTITY MODEL

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

POSTREPLY ENTITY MODEL

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

DELETEPOST METHOD/LOGIC

    public void DeletePost(Post post)
    { 

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

        if (deletePost != null)
        {

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

    }

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

    }

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

    }

回答1:


You need to add relation between entities in your DBContext

This may be One-to-Many, One-to-One, Many-to-Many

You can find detailed documentation here

Edit:

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

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

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



来源:https://stackoverflow.com/questions/61663993/how-to-delete-an-item-in-a-entity-and-delete-all-other-items-in-b-entity-whi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!