Deleting an item with entity framework

前端 未结 2 1230
萌比男神i
萌比男神i 2021-02-18 23:58

I am trying to delete an object using Entity Framework and on all the tutorials on the internet I found that in order to do that you have to call the DeleteObject m

2条回答
  •  情深已故
    2021-02-19 00:26

    Are you using a DbContext or an ObjectContext? If you have a DbContext you need to use the Remove function:

    public void DeleteBook(int bookId)
        {
            Book book = (Book)bookContext.Books.Where(b => b.Id == bookId).First();
            bookContext.Books.Remove(book);
        }
    

提交回复
热议问题