错误:对象不能删除,因为它没有在ObjectStateManager发现 [英] Error: The object cannot be deleted because it was not found in the ObjectStateManager

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

问题描述

想在这里得到实体框架手柄和我打了一些速度颠簸...
我有工作正常,并进行了测试,但我的删除方法是行不通的一个get()方法:

Trying to get a handle on Entity Framework here and I am hitting some speed bumps... I have a Get() method that works fine and has been tested, but my Delete method is not working:

   public static void Delete(string name)
    {
        J1Entities db = new J1Entities();
        db.DeleteObject(Get(name));
        db.SaveChanges();
    }



不过,我得到以下错误:
错误:因为它不是在ObjectStateManager找到的对象不能被删除。

我跑了调试器和DeleteObject的内部目标是正确的......什么我错过什么?谢谢你。

I ran the debugger, and the object inside the DeleteObject is correct... what am I missing? Thank you.

推荐答案

每个EF对象是紧密关联的管理器(一个更好的词想)创建它。或者它已被相关联。既然你不及格分贝你的获取的方法,我认为获取的或者是用它自己的 J1Entities ,或对象被创建独立的(也许是反序列化)。

Each EF object is tightly associated to the manager (for want of a better word) that created it. or to which it has been associated. Since you don't pass db to your Get method, I assume that Get has either used it's own J1Entities, or the object has been created standalone (perhaps deserialized).

为了把它删除,就必须了解它的第一个的。但在这种情况下,它似乎是一个更容易的选择就是通过分贝到<$ - 这可能通过的附着的对象到经理的意思C $ C>获取,使获取在相同的情况下(做,因为分贝将自动连接的对象,它创造本身)。

In order to delete it, it must know about it first. That might mean by attaching an object to the manager - but in this case, it seems like an easier option is just to pass db into Get, so that the Get is done in the same context (since db will automatically attach objects that it creates itself).

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆