4

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.

2
  • Use the DbContext constructor with the connection string parameter. Then you can pass this from your web project and your DAL doesn't need to keep a duplicate value. Commented Mar 19, 2015 at 1:20
  • I think thats what I was trying to do, but wasn't quite sure where to implement. Can you give me a quick example please? This is something like what I tried but couldnt get to work public partial class MyEFEntities { public MyEFEntities(string connectionstring) : base(connectionstring) { } } Commented Mar 19, 2015 at 1:27

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

5 Comments

I am trying to piece together what you are asking cause its not very clear 1) yes I am getting an error message with the code described above. Could not load type 'ContosoUniversity.DAL.SchoolContext' from assembly 'ContosoUniversity'. 2) I changed the ConnectionString name to something different and get the same error as 1 4) yes, clearly stated that there is a connectionString in the config file. If I put the DAL and the Models folder in my WebUI project, everything works just fine
updated my reply....seem context with assembly mention not matching in config. if this not work then you need to provide your config file.
I tried the above code for 'databaseinitializer' from your snippet, but still was having issues. I however did just comment out the initializer and the solution runs fine and the data is displayed. So at least I know its not an issue of not being able to get the connectionstring. I am however puzzled why the SchoolInitializer class doesn't execute, it is in the Project.Entities project in the ContosoUniversity.DAL namespace.
Its essentially this example asp.net/mvc/overview/getting-started/… but I just moved the DAL into its own project
I hope my reply indicated same for your query, if yes mark it as answer.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.