Reputation: 195
I am using Entity Framework 6.2.0
with an asp.net MVC code-first approach to do a sample application.
I have enabled-migrations and update-database successfully. Consequently, the database
Vidly.Models.ApplicationDbContext.mdf
has been created in my Solution Explorer within the App_Data
folder.
I have declared a class in my Models folder as below:
public class ApplicationDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
In my Web.Config file, I have added the connection string as below:
<connectionStrings>
<add name="ApplicationDbContext" connectionString="Data Source=.;Initial Catalog=Vidly.Models.ApplicationDbContext;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
However, when I am starting the application, I am getting the following exception :
System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
I have consulted similar exceptions on stackoverflow, but could not figure out what is wrong. Could someone advise what is wrong here please ? Thanks.
Upvotes: 0
Views: 2555
Reputation: 2548
I see that this is NOT your specific case (but it IS related to exception you are having - so added this answer for the sake of completeness)
I had the same error and the cause of the problem was that I had forgotten to add providerName
to the connection string...
(in my case - coppied sql string from Azure portal, manually created connectionString
parameter in Web.config
file and forgot to add providerName parameter, so it is not that hard to have such a mistake...)
So if you are experiencing this type of exception, you should check if your connection string is correct / not missing provider name.
Upvotes: 0
Reputation: 410
You have create your Database in your App_Data Folder but you trying to access sql server that.
try this
<connectionstrings>
<add name="ApplicationDbContext" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Vidly.Models.ApplicationDbContext.mdf;Integrated Security=True;User Instance=True " providername="System.Data.SqlClient" />
</connectionstrings>
Upvotes: 1