C# 无法删除对象,因为在 ObjectStateManager 中找不到它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15945172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:33:59  来源:igfitidea点击:

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

c#.netentity-framework

提问by petko_stankoski

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?

如何解决这个问题?

采纳答案by petko_stankoski

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);

}

回答by Mehmet Ata?

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);

回答by granadaCoder

I found an answer here:

我在这里找到了答案:

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

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();
        }

回答by Moji

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:

如果您刚刚通过帖子从编辑或删除视图中收到模型或自己生成它,那么 EF 不知道它,因此您将其状态设置为“已删除”(或 EntityState.Modified 等)以通过以下方式通知 EF:

//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();

回答by Beyto

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.

就我而言,问题是我通过删除 AsNoTracking 来获取数据 AsNoTracking,它解决了。

回答by smoothumut

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

我有同样的问题。就我而言,我一直在使用两个不同的 DBContext 实例来检索数据和更新/删除。然后我对同一代码块/方法/类中的所有数据库操作使用了相同的实例,然后问题解决了

回答by garish

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

就我而言,我试图删除未从数据库表“Student”中提取的对象。删除对象的错误方法:

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

var objStudent=新学生();db.Student.Remove(objStudent);

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

所以要注意这一点。要删除的对象应该来自表

回答by saiyyed shafique

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

我在这里找到了答案。我也试过它的工作:

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