I have an ASP.NET MVC 3 application and in my project I use ADO.NET Entity Data Model.
For access to data I use intermediate class (DataManager):
public class DataManager
{
private static mrhomeEntities _dataContext;
public DataManager()
{
_dataContext = new mrhomeEntities();
}
private OptionStorageRepository _optionStorageRepository;
public OptionStorageRepository OptionStorage
{
get { return _optionStorageRepository ?? (_optionStorageRepository = new OptionStorageRepository(_dataContext)); }
}
}
In OptionStorageRepository class I have 1 method that returns a list of options:
public IQueryable<OptionStorage> List()
{
return _dataContext.OptionStorage;
}
Such classes I have more than 15 (with this structure) to get and edit data in database.
Still I have my ControllerFabrick, where I pass a new object of DataManager class:
public class ControllerFabricFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return Activator.CreateInstance(controllerType, new DataManager()) as IController;
}
}
Also I registered my controller fabrick in Global.asax like this:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new ControllerFabricFactory());
}
And in all Controllers I receive this parameter in constructor and using in my action methods:
public class HomeController : Controller
{
private readonly DataManager dm;
public HomeController(DataManager dm)
{
this.dm = dm;
}
[HttpGet]
public ActionResult Main()
{
Page page = dm.Page.Details(mainPageName);
PageNews pageNews = new PageNews();
pageNews.page = page;
pageNews.NewsList = dm.News.List(5);
pageNews.PopularProject = dm.Project.GetPopularProject();
if (pageNews.PopularProject != null)
{
var list = pageNews.PopularProject.ProjectPictures.Where(p => p.ProjectPictureTypes.Id == 1).ToList();
if (list.Count > 0)
{
ViewData["img"] = list[0].ThumbPath;
}
}
foreach (var item in dm.Page.List())
{
ViewData[item.Name] = item.ShortPageText;
}
ViewData["projects"] = dm.Project.GetMainPageProjects();
ViewData["MainText"] = dm.Page.Details("maintext").PageText;
return View(pageNews);
}
}
And my question:
When I test my application in web browser I have strange errors, that contains information about connection to database or retrieving data, I have collection of my errors below:
- The connection was not closed. The connection’s current state is
connecting. - Invalid attempt to read when no data is present
- ExecuteReader requires an open and available Connection. The
connection’s current state is open. - New transaction is not allowed because there are other threads
running in the session. - An item with the same key has already been added.
Maybe I used wrong structure for get the data?
Per question: