Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Express database file auto-creation error in MVC 4 - But i DON'T want to use SQL Server Express

I've just deployed a new application in ASP.Net MVC 4. I use a SQL Server 2008 R2 (NOT SQL Express).

It worked good for the first 10 minutes, then I did a little change in the code and re-deployed it.

Now, whenever I try to access a page that uses SimpleMembership, I get this error:

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

But, I do not use SQL Server Express. In my web.config I've set all the connection strings as follows:

<add name="ApplicationServices" 
     connectionString="Server=myServer;Database=myDB;User Id=myUserID;Password=myPWD;" 
     providerName="System.Data.SqlClient" />

<add name="ApplicationServices" 
     connectionString="Server=myServer;Database=myDB;User Id=myUserID;Password=myPWD;" 
     providerName="System.Data.SqlClient" />

Why does it keep trying to create a SQL Server EXPRESS database?

like image 644
Carmine Giangregorio Avatar asked Apr 08 '13 22:04

Carmine Giangregorio


1 Answers

When I encountered this error, it turned out to be simple. I finally figured out that in my _Layout.cshtml I was referencing User.IsInRole("role") but after a Request.isAuthenticated. My code looked something like @if (Request.isAuthenticated && User.IsInRole("role")). So basically if I wasn't signed in the home page would render since it would not traverse to the IsInRole() call (which requires the simple membership to be initialized, otherwise I would get the error you mention here. So basically I needed to ensure that each and every controller that uses a view that extends the _Layout file and could have unauthenticated users, needs to have a [InitializeSimpleMembership]. Or one of the various ways to Initialize Simple Membership.

So something like

namespace ProjectName.Controllers
{
    [InitializeSimpleMembership]
    public class HomeController : Controller {
...

Also, I would often Ctrl + F5 to start the application and then use "Rebuild Solution" after. When I refreshed a page that wasn't the root home page, I would get the error until I would call the root home page. At that point, everything would start working again.

Hopefully this will help you or someone. I banged my head for at least an hour or two.

like image 87
SeeTrai Avatar answered Sep 30 '22 02:09

SeeTrai