2

I have a sample .NET Core 2.1 application which is using IIS Express and a LocalDB, but whenever I run the WebAPI application I get the below error:

System.Data.SqlClient.SqlException (0x80131904): 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: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.

My connection string is:

"ConnectionStrings": {
     "DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB,1433;AttachDbFilename=C:\\temp\\gitprojects\\TokenAuthAPI\\TokenAuthAPI\\TokenAuth.mdf;Integrated Security=True"
}

I initially did not have the .mdf path or, 1433 but I added those after checking online, but still same error.

In Visual Studio under SQL Server Object Explorer I made the database and see it, also when I run the command (in command prompt) SQLLocalDb info MSSQLLocalDB I see it running.

If I right click and check the properties of the database in SQL Server Object Explorer and use that connection string, I get the same error:

Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TokenAuth;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

Is this related to IIS permissions?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
Paritosh
  • 4,243
  • 7
  • 47
  • 80
  • Try using Server instead of DataSource: `Server=MyPC\\SQLEXPRESS;Initial Catalog=TokenAuth;Integrated Security=True;` Additionally try two `\\` instead of one. Lastly, check that the server name is correct. Usually IIS Express has a servername of ' \\SQLEPXRESS ' – CorrieJanse Sep 06 '21 at 00:15
  • 1
    The problem is basically that LocalDB needs to run in the context of a human Windows user, because it's storing data in their User Profile structure. There are some work around for this, such as a Shared Instance of LocalDB, but you're almost always better off using SQL Server Express or a higher edition. See [this previous SO answer](https://stackoverflow.com/a/66388986/390122) for more details. – AlwaysLearning Sep 11 '21 at 08:44
  • [Does your SQL Server service is running](https://support.solarwinds.com/SuccessCenter/s/article/Exception-A-network-related-or-instance-specific-error-while-establishing-a-connection-to-SQL-Server-EDIT?language=en_US) ? – Jason Pan Sep 13 '21 at 10:00
  • @JasonPan Yes, as i'm able to connect through SSMS, using the same server and user/pass. I have EF migrations in the project and those run fine, they are able to make changes to the db. – Paritosh Sep 15 '21 at 19:30
  • There's a more detailed write up here, and the recommendation is also to use SQL Server Express, not LocalDB. https://stackoverflow.com/questions/36703854/cannot-connect-to-localdb I'm sure you could get it working but it's probably a quicker path to just use SQL Express – Nick.Mc Oct 11 '21 at 04:09

1 Answers1

3

You must add a pool As long as the AppPool name actually exists, the login should now be created.

How I've gotten it to work is:

  1. In SQL Server Management Studio, look for the Security folder (the security folder at the same level as the Databases, Server Objects, etc. folders...not the security folder within each individual database)
  2. Right click logins and select "New Login"
  3. In the Login name field, type IIS APPPOOL\YourAppPoolName - do not click search
  4. Fill whatever other values you like (i.e., authentication type, default database, etc.)
  5. Click OK

Or

CREATE LOGIN [IIS APPPOOL\MyAppPool] FROM WINDOWS;
CREATE USER MyAppPoolUser FOR LOGIN [IIS APPPOOL\MyAppPool];
Reza Esmaeli
  • 146
  • 7