【问题标题】:The object cannot be deleted because it was not found in the ObjectStateManager?无法删除对象,因为在 ObjectStateManager 中找不到它?
【发布时间】:2012-08-06 03:25:39
【问题描述】:

我正在使用 EF4.1 从我的数据库中删除一个对象:

public virtual void Delete(T entity)
{
  _entities.CreateObjectSet<T>().DeleteObject(entity);
}

出现错误:

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

【问题讨论】:

标签: entity-framework c#-4.0


【解决方案1】:

如果您尝试删除的对象未附加到当前上下文,则会出现此错误。要获取附加对象,您需要在上下文中从数据库中查询对象,或者手动调用附加(在删除之前)将对象放入上下文中。

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多