dotnet / aspire

Tools, templates, and packages to accelerate building observable, production-ready apps
https://learn.microsoft.com/dotnet/aspire
MIT License
3.85k stars 462 forks source link

Database container with multiple database schema #3799

Open wangkanai opened 6 months ago

wangkanai commented 6 months ago

Currently I can only add a single database schema per container.

using Gsb.AppHost.Constants;

var builder = DistributedApplication.CreateBuilder(args);

var sqlMySql = builder.AddMySql(ResourceConstants.SqlMySql)
                      .WithEnvironment(DatabaseConstants.MySqlConnection, ResourceConstants.SqlMySql)
                      .AddDatabase(SchemaConstants.Configuration);

Preferred solution with multiple database schema per container.

using Gsb.AppHost.Constants;

var builder = DistributedApplication.CreateBuilder(args);

var sqlMySql = builder.AddMySql(ResourceConstants.SqlMySql)
                      .WithEnvironment(DatabaseConstants.MySqlConnection, ResourceConstants.SqlMySql)
                      .AddDatabase(SchemaConstants.Configuration)
                      .AddDatabase(SchemaConstants.Customers)
                      .AddDatabase(SchemaConstants.Laywers);
davidfowl commented 6 months ago

You can add multiple databases just fine, if you want to get a reference to those database then you can't use the fluent API:

var sqlMySql = builder.AddMySql(ResourceConstants.SqlMySql)
                      .WithEnvironment(DatabaseConstants.MySqlConnection, ResourceConstants.SqlMySql);
var configDb = sqlMySql.AddDatabase(SchemaConstants.Configuration);
var customersDb = sqlMySql.AddDatabase(SchemaConstants.Customers);
var lawyersDb = sqlMySql.AddDatabase(SchemaConstants.Laywers);
wangkanai commented 6 months ago

Thank you very much, @davidfowl. It took a while to come to your suggestion.

var builder = DistributedApplication.CreateBuilder(args);

var sqlPassword = builder.CreateStablePassword(ResourceConstants.SqlPassword, lower: true, upper: true, numeric: true,
                                               special: true, minLower: 1, minUpper: 1, minNumeric: 1, minSpecial: 1);

var sqlMySql = builder.AddMySql(ResourceConstants.SqlMySql, sqlPassword, 60100)
                      .WithEnvironment(ResourceConstants.SqlMySql, ResourceConstants.SqlMySql);

var dbConfiguration = sqlMySql.AddDatabase(SchemaConstants.Configuration);
var dbCustomer      = sqlMySql.AddDatabase(SchemaConstants.Customer);

image

Now it works perfectly.

Thank you much David!

Kralizek commented 6 months ago

Would it be possible for Aspire to actually create the databases in the server?

I noticed that the mssql hosting component allows me to define databases but they are not created.

This means that the connection string passed to the projects can't be used to connect to the database from tools like datagrip or mssql management studio and you need to change the database name to "master".

Also, it would be cool if when inspecting a database resource in the database, we could find the connection string there.