I am having an error on my web page:
{"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."}
It occours herein my view:
@model Service_Monitor_Web_Interface.Models.DashBoardViewModel
...
=> @Html.DisplayFor(modelItem => item.Service.Name)
Here is my ViewModel class:
public class DashBoardViewModel
{
public int NumberOfUsers { get; set; }
public virtual IEnumerable<ApplicationUser> recentUsers { get; set; }
public virtual IEnumerable<UserActivityLog> logs { get; set; }
public virtual IList<Service_Monitor_Domain.Entities.ServiceStatusHistory> serviceStatus { get; set; }
}
Here is my controller for the class:
public ActionResult Index()
{
DashBoardViewModel vm = new DashBoardViewModel();
var dbs = new EFServiceStatusHistoryRepository();
vm.serviceStatus = dbs.GetAllEnabledLatestStatusLog();
return View(vm);
}
and here is my function that access the db:
public List<ServiceStatusHistory> GetAllEnabledLatestStatusLog()
{
List<ServiceStatusHistory> list = null;
try
{
using (var db = new EFDbContext())
{
//var results = db.ServiceStatusHistory.Where(h => h.Service.Enabled)
// .OrderByDescending(h => h.Id)
// .GroupBy(h => h.Service.Id)
// .Select(grp => grp.FirstOrDefault());
var results = db.ServiceStatusHistory
.Where(x => x.Service.Enabled)
.GroupBy(x => x.Service)
.Select(x => x.OrderByDescending(y => y.time).FirstOrDefault());
list = results.ToList();
}
return list;
}
catch
{
//Error
}
return list;
}
Here is my entity:
public class ServiceStatusHistory
{
[Key]
[Required]
public int Id { get; set; }
// Forenkey to service
[Required]
public virtual Service Service { get; set; }
[Required]
public ServiceStatus Status { get; set; }
[Required]
public string Messages { get; set; }
[Required]
//Time Logged in the db
public DateTime time { get; set; }
[Required]
//Time the service last called the update method on the client
public DateTime LastUpdateTime { get; set; }
}
I think it has something to do with lazy loading. But I have not had this problem before when querying the same table? However it was just a simple select query.