Reputation: 5634
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.
Upvotes: 4
Views: 7211
Reputation: 5634
The code I had in the initial question worked like a charm even with the DAL split into the different project. The issue was where the EF Code First Initializer wasn't getting executed, this was because I didn't have the correct assembly in the web.config file when I moved the initializer code to its own project. The modified web.config should look like this (I was missing the .Entities where the DAL is in a project called ContosoUniversity.Entities.
<entityFramework>
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.Entities">
<databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity.Entities" />
</context>
</contexts>
Upvotes: 0
Reputation: 417
By default entry point solution check web.config with connectionstring and it goes checking for reference project.
<entityFramework>
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.DAL">
<databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity.DAL" />
</context>
</contexts>
</entityFramework>
Upvotes: 0
Reputation: 14250
This is basically how I set up my solutions. I can let the web project web.config or test project app.config define the connection string.
Data Project
public class SchoolContext : DbContext
{
public SchoolContext(string connectionString)
: base(connectionString) { }
}
Web Project
web.config
<configuration>
<connectionStrings>
<add name="SchoolContextDb" connectionString="Data Source=..." />
</connectionStrings>
<appSettings>
<add key="SchoolContext" value="SchoolContextDb" />
</appSettings>
...
</configuration>
usage
string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SchoolContext"];
SchoolContext db = new SchoolContext("Name=" + ConnectionString);
I use a DI container so I only really look up the connection string once in the application start up code. But you could make the ConnectionString
variable global or set in a base controller.
Upvotes: 2