Code First entity framework connection string

Suneel Dixit picture Suneel Dixit · Oct 8, 2012 · Viewed 29.6k times · Source

I have 3 class projects in my solutions. 1. MVC4 project 2. Domain 3. Tests

In the MVC4 project I have added a web.config file with the connection string as

<add name="EfDbContext"  connectionString="Data Source=.;Initial Catalog=SportsStore;Integrated Security=true;" providerName="System.Data.SqlClient"/>

I have a class by name EfDbContext in Domain project which inherits DbContext.

When I view the website in browser, I get the server not found error.

I debugged the EfDbContext class and found that the Database.Connection.ConnectionString is set to \SqlExpress with database as EfDbContext.

Why is that?

Answer

Adam Tuliper - MSFT picture Adam Tuliper - MSFT · Oct 8, 2012

You must have your connect string as the FULL namespace to your context when your context class is in a different project

For example:

<add name="YourClassProject.EfDbContext"  connectionString="Data Source=.;Initial Catalog=SportsStore;Integrated Security=true;" providerName="System.Data.SqlClient"/>