If you have a look in the WithReference function of IResourceBuilder you will note the following code:
return builder.WithEnvironment(context =>
{
var connectionStringName = resource.ConnectionStringEnvironmentVariable ?? $"{ConnectionStringEnvironmentName}{connectionName}";
context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resource, optional);
});
https://github.com/dotnet/aspire/blob/e9688c40ace2271cef6444722abdf2f028ee1229/src/Aspire.Hosting/ResourceBuilderExtensions.cs#L448-L465
So to override the environment variable set we just need to use the same .WithEnvironment function but set our own Custom name. Here is an Example of how it would look in your example:
orderApi
.WithReference(orderApiDatabase)
.WithEnvironment(context => // using custom place for Db ConnectionString
{
context.EnvironmentVariables["MyCustomSection__Database__OrderApi__ConnectionString"] =
new ConnectionStringReference(orderApiDatabase !.Resource, false);
});
.WaitFor(orderApiDatabase);