简体   繁体   中英

Accessing EF ConnectionString from different project within same solution MVC

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.

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.

By default entry point solution check web.config with connectionstring and it goes checking for reference project.

  1. with this code snippet, its not getting more details, you getting any error.
  2. can you verify if you change dbcontext name what error you getting. by default if it not set then it taking class name define with dbcontext class.
  3. verify ...
  4. I hope you already set settings in config file. verify context and namespace

  <entityFramework> <contexts> <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.DAL"> <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity.DAL" /> </context> </contexts> </entityFramework> 

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM