Welcome to the Microsoft Q&A Platform!
Thank you for reaching out about Azure SQL Trigger Can't use Environment Variable as connectionstring.
The error you're encountering (CS0182: an attribute must be a constant expression) occurs because in C#, attributes require constant values at compile time. The Environment.GetEnvironmentVariable() method returns a runtime value, which is not a constant.
You can’t pass the connection string as an environment variable directly within the attribute.
Instead of using the environment variable in the attribute, you can pass the connection string dynamically within the code by setting up the connection string at runtime.
For example, try to modify your function to retrieve the connection string inside the method:
[Function("WarningTrigger")]
public async Task Run(
[SqlTrigger("[dbo].[SensorWarnings]")] IReadOnlyList<SqlChange<Model.SensorWarning>> changes,
FunctionContext context)
{
string connectionString = Environment.GetEnvironmentVariable("dbconnection");
// Now use the connection string for your SQL query logic
SensorDatabaseQuery query = new SensorDatabaseQuery(connectionString);
await query.GetData();
}
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.