Cannot call DeleteObject in Entity structure - missing assembly reference?

I am trying to delete an object in my ASP.NET MVC3 / Code-first Entity Framework application, but I do not have the required parameters, because it causes a "does not contain a definition for DeleteObject" error. Does anyone know if I am missing a build 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 links:

 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 that for System.Data.Entity it would be enough to call DeleteObject, but intellisense hardly brings up any parameters - only Dispose, Entry, SaveChanges and Set

Edit: Here is also my code to access the repository:

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

Edit 2: Here is an image of a folder with my links:

enter image description here

thanks

+6
source share
4 answers

The documentation for DbContext in EF4.1 seems to show that it does not include the delete method in this class: http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v= vs.103) .aspx ...

This question looks similar - there might be some help: MVC 3 EF 4.1 dbContext - Removing a one-to-many data object with an odd foreign key ratio

+4
source

DbContext does not have a DeleteObject() method. Instead, use the Remove() method to clean the object from DbSet , and 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 
+24
source

You can use this format:

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

Delete () only works in DBContext ..

0
source

In Mvc 5, use DeleteRange instead of DeleteObject when deleting a list.

RemoveRange ();

0
source

Source: https://habr.com/ru/post/893170/


All Articles