petko_stankoski
petko_stankoski

Reputation: 10713

The object cannot be deleted because it was not found in the ObjectStateManager

I have this code which normally works:

db.myTable.DeleteObject(myCurrent);

And I got this error:

The object cannot be deleted because it was not found in the ObjectStateManager.

The same ingredients IS in the table in the database.

I tried this:

db.myTable.Attach(myCurrent);
db.myTable.DeleteObject(myCurrent);

And I got another error:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker. 

How to fix this?

Upvotes: 21

Views: 50550

Answers (8)

granadaCoder
granadaCoder

Reputation: 27852

I found an answer here:

http://www.entityframeworktutorial.net/delete-entity-in-entity-framework.aspx

Here is the "disconnected" version, which I needed;

       Student stud = null;

        using (SchoolDBContext ctx = new SchoolDBContext())
        {
            stud = (from s in ctx.Students
                    where s.StudentName == "Student1"
                        select s).FirstOrDefault();
        }

        using (SchoolDBContext newCtx = new SchoolDBContext())
        {
            newCtx.Students.Attach(stud);
            newCtx.Students.DeleteObject(stud);
            //you can use ObjectStateManager also
            //newCtx.ObjectStateManager.ChangeObjectState(stud, System.Data.EntityState.Deleted);
            int num = newCtx.SaveChanges();
        }

Upvotes: 3

saiyyed shafique
saiyyed shafique

Reputation: 1

I found an answer here. I also tried its working :

dbEntities.Entry(CurrentObj).State = EntityState.Deleted;                
dbEntities.SaveChanges();

Upvotes: 0

Web Developer India
Web Developer India

Reputation: 677

In my case I was trying to delete an object that is not fetched from the database table "Student". Wrong way to delete object:

var objStudent= new Student(); db.Student.Remove(objStudent);

So take care of this. Object to delete should come from the table

Upvotes: 1

smoothumut
smoothumut

Reputation: 3491

I had the same problem. In my case I had been using two different DBContext instances for retriving data and update/remove. Then I have used the same instance for the all db actions in the same codeblock/method/class, then problem resolved

Upvotes: 0

Manoj
Manoj

Reputation: 6000

if you just received model from edit or delete view by post or generated it yourself then EF doesn't know about it so you set its state to "Deleted" (or EntityState.Modified etc) to inform EF by:

//generate it yourself if not posted from edit/delete view
//var model = new Model { Id = 123 };

//set to delete
db.Entry(model).State = EntityState.Deleted; // or EntityState.Modified for edit etc.
db.SaveChanges();

Upvotes: 11

Beyto
Beyto

Reputation: 379

Although this question is an old question, But it may help another people with same error,

In my case, the problem was I Fetched data AsNoTracking, by removing AsNoTracking it solved.

Upvotes: 4

petko_stankoski
petko_stankoski

Reputation: 10713

The other answer didn't work, so here's how I fixed it.

Previously I had:

public void ok(myTable myCurrent)
{
    //delete entries from other tables in relationship with myTable
    db.myTables.DeleteObject(myCurrent);

}

I fixed it with this:

public void ok(int current_id)
{
    //delete entries from other tables in relationship with myTable
    var y = (from x in db.myTables where x.id == current_id select x).First();
    db.myTables.DeleteObject(y);

}

Upvotes: 10

Mehmet Ataş
Mehmet Ataş

Reputation: 11549

The problem is you cannot delete (or remove) detached entities and cannot attach an entity twice. You need something like below.

var entry = db.Entry(myCurrent);
if (entry.State == EntityState.Detached)
    db.myTable.Attach(myCurrent);
db.myTable.Remove(myCurrent);

Upvotes: 27

Related Questions