17

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?

3 Answers 3

17

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"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Also note that with EF6 if you want to use shared database for multiple contexts make sure your context class calls base("your content name as listed above in this post") as that will be the name of the connect string looked for as well.
11

You should create your DbContext with your connection string name passing to the constructor

public class UnicornsContext : DbContext
{
    public UnicornsContext()
        : base("EfDbContext")
    {
    }
}

http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

1 Comment

This is the best solution for me at least :) Thanks.
1

Finally, I realized that I added the connection entry in the web.config file created by VS in Views folder.

My bad.

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.