I have GeneralRepository
Here is code
public class GeneralRepository
{
public IEnumerable<Site> GetSites()
{
using (TraxgoDB ctx = new TraxgoDB())
{
return ctx.Sites;
}
}
public Customer GetCustomer(int customerID, bool main = false)
{
using (TraxgoDB ctx = new TraxgoDB())
{
var customer = ctx.Customers.First(c => c.ID == customerID);
if (main) customer = (customer as SubUser)?.MainUser ?? customer;
return customer;
}
}
public Dictionary<int,int> GetCustomerIDDictionary()
{
using (TraxgoDB ctx = new TraxgoDB())
{
return ctx.Customers.ToDictionary(
c => c.ID,
c => (c as SubUser) != null ? (int) (c as SubUser).MainUserID : c.ID
);
}
}
}
In Global.asax i have this code that using repo and method from repo
private ConcurrentDictionary<string, SiteViewModel> InitializeSites()
{
var repository = new GeneralRepository();
var siteDictionary = repository.GetSites().ToDictionary(
s => s.HostName.ToLower(),
s => SiteViewModel.CreateCustomTrackerwebSite(s.HostName, s)
);
return new ConcurrentDictionary<string, SiteViewModel>(siteDictionary);
}
When I run website I have this error
The operation cannot be completed because the DbContext has been disposed.
In this row
var siteDictionary = repository.GetSites().ToDictionary
How I can solve this error?