0

I'm getting the exception:

"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

Only when I try to add the object or some properties of that object to @Html.ActionLink.

I have reviewed my code couple of times and can't find anything unusual any suggestions on what might be causing this exception?

Thank you!

UserManagerResult.cs

public class UserManagerResult {
    public bool Success{get; set;}
    public string ErrorMessage{get; set;}
    public Account User{get; set;}
    public List <Account> UserList{get;}
    public UserManagerResult() {
        Success = false;
        UserList = new List <Account>();
    }

}

UserManager.cs

    public static UserManagerResult GetUserList() {
        UserManagerResult userManagerResult = new UserManagerResult();
        try {
            using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) {
                foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) {
                    userManagerResult.UserList.Add(account);
                }
                return userManagerResult;
            }
        }
        catch (Exception ex) {
            userManagerResult.ErrorMessage = ex.Message;
            return userManagerResult;
        }
    }

DeveloperViewModel.cs

public class DeveloperViewModel {
    [Display(Name = "New Role")]
    public string NewRole{get; set;}
    [Display(Name = "Remove Role")]
    public List <SelectListItem> RoleList{get; set;}
    [Display(Name = "User list")]
    public List <Account> UserList{get; set;}
}

UserManagerController.cs

 public ActionResult Developer(DeveloperViewModel model) {
        UserManagerResult getUserList = UserManager.GetUserList();
        model.UserList = getUserList.UserList.ToList();
        return View(model);
    }

The view I'm using

                 @{
                foreach (Account account in Model.UserList.ToList()) {
                    <tr>
                        <th scope="row">--</th>
                        <td>@account.Email</td>
                        <td>@account.LastName</td>
                        <td>@account.FirstName</td>
                        <td>
                            @Html.ActionLink("Remove", "Remove", account)
                        </td>
                    </tr>
                }
            }
우두머리
  • 545
  • 2
  • 18
alentor
  • 27
  • 3
  • 12
  • Show me your Account entity. It's probably lazy loading in action. Convert your `account` in `userManagerResult.UserList.Add(account);` to some projection – Sergey Shuvalov Feb 12 '16 at 15:52
  • I have worked around the issue by projection the needed information from Account to a new class AccountViewModel, to expose the view only the relevant information. – alentor Feb 12 '16 at 16:26

1 Answers1

0

In your GetUserList(), try this:

 foreach (Account account in alenMotorsDbEntities.Accounts.Include(i=>i.YourOtherEntity).Include(i=>i.AnotherEntity).ToList())

And keep adding all related entities that is relevant.

  • I have tried using include but I don't have what to include.. because I want the entire alenMotorsDbEntities.Accounts to List http://tiny.cc/36628x – alentor Feb 12 '16 at 07:51