Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First entity framework connection string

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?

like image 968
Suneel Dixit Avatar asked Oct 08 '12 03:10

Suneel Dixit


1 Answers

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"/>
like image 82
Adam Tuliper Avatar answered Oct 10 '22 22:10

Adam Tuliper