嘗試刪除用戶和關聯對象時出現錯誤消息。錯誤消息是該對象無法刪除,因爲它在ObjectStateManager中找不到。如何刪除MVC/Entity Framework中的對象:由於在ObjectStateManager中找不到對象,因此無法刪除對象
控制器
[Authorize, HttpPost]
public ActionResult DeleteUser(string UserName)
{
User user = _userRepository.GetByUserName(UserName);
if (user == null)
return new FileNotFoundResult();
_repository.DeleteUser(user);
return RedirectToAction("Index");
}
REPOSITORY
public void DeleteUser(User user)
{
foreach (Follower follower in user.Followers)
_db.Followers.DeleteObject(follower);
foreach (Comment comment in user.Comments.ToList())
_db.Comments.DeleteObject(comment);
_db.Users.DeleteObject(user);
}
我這麼想嗎?
您從'_userRepository'獲取用戶,但使用'_repository'刪除。這是一個錯字嗎? – 2011-03-23 03:12:02