用实体框架删除一个项目 [英] Deleting an item with entity framework
问题描述
DeleteObject
方法上下文。我尝试这样做,但似乎我没有 DeleteObject
方法。 这是我的代码:
public void DeleteBook(int bookId)
{
Book book =(Book)bookContext.Books.Where(b = > b.Id == bookId).First();
bookContext.DeleteObject(book);
}
这是我得到的错误:
$ $$$$$$不含DeleteObject的定义,而没有扩展方法DeleteObject接受DataAccess.Models类型的第一个参数。可以找到BooksEntities(您是否缺少using指令或程序集参考?)
我做错了什么?
您是否使用DbContext或ObjectContext?如果您有DbContext,您需要使用删除功能:
public void DeleteBook(int bookId)
{
Book book = (Book)bookContext.Books.Where(b => b.Id == bookId).First();
bookContext.Books.Remove(book);
}
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
method on the context. I tried doing that but it seems I have no DeleteObject
methods.
Here is my code:
public void DeleteBook(int bookId)
{
Book book = (Book)bookContext.Books.Where(b => b.Id == bookId).First();
bookContext.DeleteObject(book);
}
This is the error I get:
'DataAccess.Models.BooksEntities' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'DataAccess.Models.BooksEntities' could be found (are you missing a using directive or an assembly reference?)
What am I doing wrong?
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);
}
这篇关于用实体框架删除一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!