Skip to content

Commit 8159949

Browse files
committed
Fix database connection logic for Azure deployment - Updated Program.cs to properly read connection string from Azure App Service - Added fallback to hardcoded connection string if App Service config fails - Fixed the issue where app was falling back to SQLite instead of Azure SQL
1 parent 52b44bc commit 8159949

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

BookReviewApp.Web/Program.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@
3535

3636
if (isAzure)
3737
{
38-
// Use Azure SQL Database connection string
39-
var server = Environment.GetEnvironmentVariable("AZURE_SQL_SERVER") ?? "your-server.database.windows.net";
40-
var database = Environment.GetEnvironmentVariable("AZURE_SQL_DATABASE") ?? "BookReviewAppDB";
41-
var userId = Environment.GetEnvironmentVariable("AZURE_SQL_USERID") ?? "your-username";
42-
var password = Environment.GetEnvironmentVariable("AZURE_SQL_PASSWORD") ?? "your-password";
38+
// Try to get connection string from Azure App Service configuration
39+
connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
4340

44-
connectionString = $"Server={server};Database={database};User Id={userId};Password={password};TrustServerCertificate=true;";
45-
Console.WriteLine("Using Azure SQL Database connection");
41+
if (string.IsNullOrEmpty(connectionString))
42+
{
43+
// Fallback to hardcoded Azure SQL Database connection string
44+
var server = "bookreviewapp-sql-1755367448.database.windows.net";
45+
var database = "BookReviewAppDB";
46+
var userId = "bookreviewadmin";
47+
var password = "BookReviewApp2025!";
48+
49+
connectionString = $"Server=tcp:{server},1433;Initial Catalog={database};Persist Security Info=False;User ID={userId};Password={password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
50+
Console.WriteLine("Using hardcoded Azure SQL Database connection");
51+
}
52+
else
53+
{
54+
Console.WriteLine("Using Azure App Service connection string");
55+
}
4656
}
4757
else
4858
{

0 commit comments

Comments
 (0)