I have an instance of Azure Sql DB, there is no issue when I run the web app locally. The connection to Azure SQL DB is made successfully locally and I get the data as well. Though when I publish the web app to Azure, I get 500 Internal server error (details of the error below). Can someone help on suggesting some right steps. Web App is running on .Net8.0
2024-04-18 13:11:22.251 +00:00 [Error] Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Connection ID "17221764975870083192", Request ID "40000079-0000-ef00-b63f-84710c7967bb": An unhandled exception was thrown by the application.System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when sending the request to the server. (provider: Session Provider, error: 19 - Physical connection is not usable)at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry)at System.Data.SqlClient.SqlConnection.Open()at AnFramework.Data.Core.ConnectionManager.GetConnection(String databaseName)at AnFramework.Data.Core.ConnectionManager.PerformDatabaseOperation(Int32 opCode, IConnectionManaged targetObject, DbOperationOptions options)at
The error connectivity issue between Azure Web App and Azure SQL Database occurs when there are no added service dependencies and Configuration details in Azure.
Microsoft.EntityFrameworkCore.SqlServer
and Microsoft.EntityFrameworkCore.Tools
packages from the NuGet .public class Todo
{
public int ID { get; set; }
public string? Description { get; set; }
[DisplayName("Created Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime CreatedDate { get; set; }
}
appsettings.json
with the following:Server=tcp:myserver.database.windows.net,1433;Initial Catalog=myDataBase;User Id=mylogin@myserver;Password=myPassword;Encrypt=True;Connection Timeout=30;
Use the package manager console to introduce a new migration to the project utilizing Entity Framework.
Add-Migration YourMigrationName
.
In EF Core, updating a database with a migration entails applying changes through the Up
method in the migration file, or rolling back changes via the Down
method. To execute or reverse a migration, you would use the Update-Database
command.
Add the configuration name AZURE_SQL_CONNECTIONSTRING
with your SQL connection string value and the name AZURE_REDIS_CONNECTIONSTRING
with your Azure Cache for Redis connection string value in the web application settings.
Add the Configuration Name AZURE_SQL_CONNECTIONSTRING
with the Value YourConnectionstrings and NameAZURE_REDIS_CONNECTIONSTRING
with the Value YourAzureCacheRedisConnectionString in the Web App.
Add the service dependencies and deploy the application to the Azure Web App.
Azure:
For Azure SQL, refer to this documentation for Creating and Deploying .NET Minimal API and SQL Database on Azure