2011-07-20 28 views
6

我想在我的asp.net MVC3 /代碼優先的實體框架應用程序中刪除一個對象,但我似乎沒有必要的選項,因爲它帶來了一個「不包含DeleteObject的定義」錯誤。任何人都知道我是否缺少裝配參考。這裏是我的倉庫代碼:無法在實體框架中調用DeleteObject - 缺少程序集引用?

private dbContext db = new dbContext(); 

public void DeleteAccessDetails(AccessDetails details) 
{ 
    db.DeleteObject(details); //error here as DeleteObject isn't recognised 
} 

這裏是我的引用:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using MySite.Models; 
using System.Data; 
using System.Data.Objects; 
using System.Web.Mvc; 
using System.Data.Entity; 

我想有System.Data.Entity的就足夠了,彈出DeleteObject的,但智能感知很難造就任何選項 - 只處置,錄入,調用SaveChanges和設置

編輯:這也是我訪問的庫代碼:

Repository rep = new Repository(); 
AccessDetails paymentUpdate = rep.GetPaymentByID(item.AccessDetailsTableID); 
rep.DeleteAccessDetails(paymentUpdate); 

編輯2:這裏是我的引用文件夾的圖像:

enter image description here

感謝

+0

正確的是,我的引用文件夾中沒有System.Data.Objects。當我查看添加引用部分(.NET選項卡)時,它不在那裏。 –

回答

23

DbContext沒有DeleteObject()方法。取而代之的是,使用Remove()方法從DbSet中清除對象,然後保存更改。

dbContext db = new dbContext(); // Arrange the context 

Department dept = db.Departments.Single(p => p.Id == "1"); // Find the item to remove 

db.Departments.Remove(dept); // Remove from the context 

b.SaveChanges(); // Delete from the database 
+0

如果存在不可空的外鍵約束,Remove()將不起作用。 – NoRMO

0

您可以使用此格式:

context.Entry(temp).State = EntityState.Deleted; 

刪除()只在的DbContext工作..

0

在MVC 5 使用RemoveRange而不是DeleteObject的同時刪除列表。

RemoveRange();