Replies: 2 comments
-
Hello @CarlGraff, var azureSql = builder.Environment.IsDevelopment()
? builder.AddConnectionString("LocalSqlConnection")
: builder.AddSqlServer("aspiretest-sql").AddDatabase("aspiretest-db"); This way you keep one code path, just different config per environment. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply, I did try this: And it works fine when ran locally on SQL Develoment addition assuming the database is already created, but, there is an issue when publishing to azure as it thinks the connectionstring is missing and prompts you for it. This is what I am working with now and it works fine in version 9.4.0: IResourceBuilder sqlDb = default; The key was finding this: So this does everything I wanted Publish mode:
Local
Next I will try local as SQL in container. Best, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been struggling to find a way to integrate SQL server that works both while running locally and publishing to the cloud.
I have watched many Youtube videos and various other documentation that describe different methods of doing this but none of them seem really that good. Perhaps I am missing something that exists that is better than these approaches.
Here is host code that allows connection to an existing local SQL development server complete with migrations working as expected and also allows publishing to provision (if needed) and perform migrations as expected. Note that is is also trivial to connect to either using a tool like SSMS.
=======================================
using Microsoft.AspNetCore.Hosting;
using System;
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
// ASPIRE TOGGLE AI PROMPT:
// Toggle Aspire mode in Program.cs to the opposite of its current setting by commenting/uncommenting lines marked ASPIRE_MODE:LOCAL and ASPIRE_MODE:PUBLISH as appropriate.
// ASPIRE_MODE:LOCAL
//var azureSql =
// builder.AddConnectionString("aspiretest-db");
//var migrations =
// builder.AddProject<Projects.AspireApp2_MigrationService>("migrationservice")
// .WithReference(azureSql);
// ASPIRE_MODE:PUBLISH
var azureSql =
builder.AddSqlServer("aspiretest-sql").AddDatabase("aspiretest-db");
var migrations =
builder.AddProject<Projects.AspireApp2_MigrationService>("migrationservice")
.WithReference(azureSql)
.WaitFor(azureSql);
var apiService = builder.AddProject<Projects.AspireApp2_ApiService>("apiservice")
.WithReference(azureSql) // API service needs the database connection
.WithReference(migrations) // API service depends on the migration service
.WaitFor(migrations) // Crucial: API service waits for migrations to complete before starting
.WithReference(cache)
.WaitFor(cache);
builder.AddProject<Projects.AspireApp2_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(cache)
.WaitFor(cache)
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
Issue with this is of course I have to Uses the appropriate code for running locally and uses the appropriate code for publishing - a hack I know but I had to weigh this against the other options that i found. Real issue is that the code analysis is very aggressive won't allow me to do things like this:
#if DEBUG
do local stuff
#else
do publish stuff
#endif
or
if (env.IsDevelopment())
do local stuff
else
do publish stuff
I am looking at this:
AzureSqlExtensions.PublishAsAzureSqlDatabase Method
and this
var data = builder.AddAzureSqlServer("data")
.RunAsContainer();
But I am not sure if these will help Any suggestions are welcome.
Beta Was this translation helpful? Give feedback.
All reactions