Connection string between IIS and SQL server

asp.net-mvc entity-framework iis sql-server

Question

I'm confused by the connection string in the Web.config file of project with Entity Framework Web API. There are a lot of variants I tried, but none of them was helping me out. Currently the connection string is

  <connectionStrings>
       <add name="DefaultConnection" connectionString="Server=.\SQLEXPRESS;Database=MyProject;User Id=John;Password=duck;" providerName="System.Data.SqlClient" />
  </connectionStrings>

I can access the database which is called MyProject in SQL Server Management Studio 2012 (even from remote), using John as username and duck as password.

The project is run by an IIS server on the same machine as the SQL server, the SQL server's instance name is SQLEXPRESS. My webpage is displayed on localhost, but the controller just replies 500 Interal server error whenever some data from the sql database is requested. It looks like there is no connection to the database.

What do I have to add or change in order to get a connection betweeen IIS and SQL server, or how can I locate better the problem's cause?

1
2
12/9/2013 3:59:40 PM

Accepted Answer

This tutorial how to setup SQL Server 2008 for an ASP.net website on IIS 7.0 brought me close to the solution.

Basically, what you need to do is

  1. Install SQL server.
  2. Allow TCP/IP connections to the SQL server.
  3. Attach your database.
  4. Create a login. I'm using SQL Authentication.
  5. Assign the user permissions to your database.
  6. Configure your database connection string.

Changing the name of my connection string helped me establish the connection finally.

<connectionStrings>
       <add name="MyProjectContext" connectionString="Server=.\SQLEXPRESS;Database=MyProject;User Id=John;Password=duck;" providerName="System.Data.SqlClient" />
</connectionStrings>

The context model required this name.

public class MyProjectContext : DbContext
{

    public MyProjectContext() : base("name=MyProjectContext")
    {
    }

    public DbSet<Model1> Model1 { get; set; }
    public DbSet<Model2> Model2 { get; set; }
}
2
12/13/2013 7:56:33 AM

Popular Answer

Use the Event Viewer to get more information on what the 500 error actually is.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow