사용자 및 관련 개체를 삭제하려고 할 때 오류 메시지가 표시됩니다. 오류 메시지는 입니다. ObjectStateManager에 개체가 없으므로 개체를 삭제할 수 없습니다.MVC/Entity Framework에서 개체를 삭제하는 방법 : ObjectStateManager에서 개체를 찾을 수 없어 개체를 삭제할 수 없습니다.
CONTROLLER
[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);
}
오전 내가 모르는 뭔가가?
을 수정하고 싶어 나는 비슷한 뭔가를해야하지만,'_repository'를 사용하여 삭제할 것입니다. 그건 오타예요? –