zoukankan      html  css  js  c++  java
  • The object cannot be deleted because it was not found in the ObjectStateManager

    What it sounds like to me is that you're getting a user from one context, and attempting to delete from another.

    E.g.

    User myUser =null;
    
    using(MyData data =newMyData()){
        myUser = data.GetUserById(1);}
     
    
    using(MyData data =newMyData()){
        data.DeleteUser(myUser);}

    The 2nd "data" doesn't know about that user, because it didn't retrieve it.

    Instead, you'd have to something like

    using(MyData data =newMyData()){
        data.Context.Entry(myUser).State=EntityState.Deleted;
        data.SaveChanges();}

    Syntax might not be exactly right, but essentially you need to set that your user object is an entity in that data context, and that it's state is Deleted.

    You'd have to do something similar i you wanted to modify an existing object (set state to EntityState.Modified)

  • 相关阅读:
    Iterator与 Enumeration
    多态性理解
    django---路由层
    django常用模块汇总
    django初识
    python常见模块
    python PEP8规范
    logging模块
    mysql一些了解的名词
    python 链接 mysql数据库
  • 原文地址:https://www.cnblogs.com/llk8/p/3515204.html
Copyright © 2011-2022 走看看