I'm trying to delete an object in my asp.net MVC3/Code-first Entity Framework application, but I don't seem to have the necessary options, as it brings up a "does not contain a definition for DeleteObject" error. Anyone know if I'm missing an assembly reference. Here is my repository code:
private dbContext db = new dbContext();
public void DeleteAccessDetails(AccessDetails details)
{
db.DeleteObject(details); //error here as DeleteObject isn't recognised
}
Here are my references:
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;
I thought having System.Data.Entity would have been enough to bring up DeleteObject, but intellisense is hardly bringing up any options - only Dispose, Entry, SaveChanges and Set
Edit: Here is also my code for accessing the repository:
Repository rep = new Repository();
AccessDetails paymentUpdate = rep.GetPaymentByID(item.AccessDetailsTableID);
rep.DeleteAccessDetails(paymentUpdate);
Edit 2: Here is an image of my references folder:
Thanks
DbContext
does not have a DeleteObject()
method. Instead of that, use the Remove()
method to clear the object from the DbSet
, then save the changes.
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
The documentation for the DbContext in EF4.1 seems to show that it doesn't include a delete method on that class: http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.103).aspx...
This question looks similar - might be some help: MVC 3 EF 4.1 dbContext - Deleting one-to-many data object with non-nullable foreign-key relation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With