In trying to practice separation of concerns, I am taking an ASP.NET MVC project and putting the Models and the DBContext into a separate project within the same solution. Now I have a Project.Web which houses the ViewModels, Controllers and Views and I have a Projects.Entities for the Models and DAL. The web.config file that has the ConnectionString attribute is in the Project.Web project.
Project.Web
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=localhost;Initial Catalog=ContosoUniversity1;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Home Controller:
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
Project.Entities
public class SchoolContext : DbContext
{
public SchoolContext() : base("SchoolContext")
{
}
My issue is that the SchoolContext connectionString name isn't getting picked up by the DBContext because its in the web.config of the other project. How do I mitigate this? Everything works fine when all the MVC components are in the same project.
public partial class MyEFEntities { public MyEFEntities(string connectionstring) : base(connectionstring) { } }