Entity Framework 4.1: How do I delete by object Id

后端 未结 4 1585
梦毁少年i
梦毁少年i 2021-02-01 15:42

I would like to know how to delete an object from Entity Framework 4.1 without first having to load the object from the database. I have found these other 2 answers on Stack Ove

4条回答
  •  时光取名叫无心
    2021-02-01 16:16

    I use the following for my deletes, works great.

    public virtual ActionResult Delete(int commentID)
    {
        var c = new Comment(){CommentID = commentID};
        db.Entry(c).State= EntityState.Deleted;
        db.SaveChanges();
        return RedirectToAction(MVC.Blog.AdminComment.Index());
    }
    

    enter image description here

提交回复
热议问题