llk8

MVC开发/常见错误集锦

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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)

posted on 2014-01-11 17:59  llk8  阅读(981)  评论(0编辑  收藏  举报