How to connect to the database after publishing to .NET Core

jewel 1,186 Reputation points
2025-05-19T15:58:29.58+00:00

I am trying to use the DotNet Core 8.0 application by publishing it on the local host. But it is not connecting to the database. Normally there is no problem. But after publishing it, there is a problem. I am seeking help from experienced people.

Community Center Not monitored
{count} votes

Accepted answer
  1. Pradeep M 9,765 Reputation points Microsoft External Staff Volunteer Moderator
    2025-05-21T06:04:20.4833333+00:00

    Hi jewel,

    If your application runs correctly in the development environment but fails to connect to the database after publishing, the issue is often related to the connection string configuration. 

    Please ensure that the correct connection string is defined in the appsettings.json or environment variables used by the published application. Unlike development, the published app does not use appsettings.Development.json. 

    Also, verify that the database is accessible from the environment where the app is hosted and that the necessary credentials and permissions are in place. 

    If the issue continues, kindly provide more details along with any error messages or screenshots. This will help us better understand the problem and offer a more accurate solution. 
    If you find the answer helpful, please click the "Accept Answer" and "Upvote" buttons.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2025-05-21T15:27:22.4066667+00:00

    When you run the app with VS it’s running under your account, which probably has database access. When you publish to IIS, it runs under the app pool account. The default app pool is a local account without network permissions, so probably cannot access the database. You need to change the app pool account to a domain account, and give the account permissions to access the database.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.