我得到了例外:
“ObjectContext实例已被释放,不能再用于需要连接的操作。”
仅当我尝试将对象或该对象的某些属性添加到@Html.ActionLink
时。
我已经多次审核了我的代码,并且找不到任何有关可能导致此异常的建议的异常情况?
谢谢!
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);
}
我正在使用的视图
@{
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>
}
}
答案 0 :(得分:0)
在你的GetUserList()中,试试这个:
foreach (Account account in alenMotorsDbEntities.Accounts.Include(i=>i.YourOtherEntity).Include(i=>i.AnotherEntity).ToList())
并继续添加所有相关的相关实体。