In Entity Framework, I can't use DeleteObject since I'm lacking an assembly reference.

asp.net-mvc c# entity-framework

Question

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:

enter image description here

Thanks

1
7
1/26/2013 4:01:04 PM

Accepted Answer

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

4
5/23/2017 11:54:22 AM

Popular Answer

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow