本文介绍了对象不能被删除,因为它没有在ObjectStateManager发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误的对象不能因为它没有在ObjectStateManager中删除。

我的code是:

 保护MyEntities sqlEntities;    公共虚拟无效删除(TEntity实体)
    {
        System.Type的T = typeof运算(TEntity)已;
        sqlEntities.DeleteObject(实体);
        sqlEntities.SaveChanges();
    }


解决方案

这意味着实体未附后(它不是由同一个上下文实例加载)。试试这个:

 保护MyEntities sqlEntities;公共虚拟无效删除(TEntity实体)
{
    sqlEntities.Attach(实体);
    sqlEntities.DeleteObject(实体);
    sqlEntities.SaveChanges();
}

I am getting this error "The object cannot be deleted because it was not found in the ObjectStateManager."

My code is:

    protected MyEntities sqlEntities;

    public virtual void Delete(TEntity entity)
    {
        System.Type t = typeof(TEntity);
        sqlEntities.DeleteObject(entity);
        sqlEntities.SaveChanges();
    }
解决方案

It means that entity is not attached (it was not loaded by the same context instance). Try this:

protected MyEntities sqlEntities;

public virtual void Delete(TEntity entity)
{
    sqlEntities.Attach(entity);
    sqlEntities.DeleteObject(entity);
    sqlEntities.SaveChanges();
}

这篇关于对象不能被删除,因为它没有在ObjectStateManager发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 16:12