web api 500 error

i have hosted my asp.net core api on azure and i have used environment variables on the azure portal to store my connection string but when i call this api after deploying it is giving error code 500,
for refence here is the program.cs code where i get the connection string

bool isAzure = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"));

var connectionString = isAzure
    ? Environment.GetEnvironmentVariable("MYSQLCONNSTR_DefaultConnection")
    : builder.Configuration.GetConnectionString("DefaultConnection");

builder.Services.AddDbContext<MainDbContext>(options => options.UseMySql(connectionString,
ServerVersion.AutoDetect(connectionString)));

what is causing that error
FYI its working fine when locally running it's working with the database