I wanted the code to be easy used by any programmer that uses SqlServer in ASPIRE , such as
var paramPass = builder.AddParameter("password", "myP@ssW0rd");
var sqlserver = builder.AddSqlServer("sqlserver", paramPass, 1433);
var db = sqlserver.AddDatabase("DepEmp")
.WithSqlPadViewerForDB(sqlserver)
.WithSqlCommand("delete","delete from Employee")
.ExecuteSqlServerScripts(DBFiles.FilesToCreate);
For this to work, I create extensions – you can see them at https://github.com/ignatandrei/aspireExtensions/blob/main/src/SqlServerExtensions/SqlExtensionsAspire/SqlServerExtensions.cs
Some problems that I solved
1. Versioning
The Aspire version was 9.3 , so my version of the nuget SqlExtensionsAspire is 93.2025.208.1818 ( dependent version and date )
2. Logging into Aspire
The code for a command to log into Aspire resource is
//db is the resource
db.WithCommand(name, name,async ecc =>
{
// Get the resource logger service to log command execution
var log= ecc.ServiceProvider.GetService(typeof(ResourceLoggerService)) as ResourceLoggerService;
if (log == null) return new ExecuteCommandResult()
{
Success = false,
ErrorMessage = $"no logging available"
};
// Get a logger instance for this specific database resource
var lg = log.GetLogger(db.Resource);
if (lg == null) return new ExecuteCommandResult()
{
Success = false,
ErrorMessage = $"on {db.Resource.Name} no logger"
};
//now you can use lg to log
3. Documentation If you add a docker to Aspire, you should read the documentation carefully
// Create SQLPad container with pre-configured connections
var sqlpad = builder
.AddContainer("sqlpad", "sqlpad/sqlpad:latest")
.WithEndpoint(5600, 3000, "http")
// Disable authentication for development ease
.WithEnvironment("SQLPAD_AUTH_DISABLED", "true")
.WithEnvironment("SQLPAD_AUTH_DISABLED_DEFAULT_ROLE", "Admin")
.WithEnvironment("SQLPAD_ADMIN", "admin@sqlpad.com")
.WithEnvironment("SQLPAD_ADMIN_PASSWORD", "admin")
// Configure connection to the target database
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__name", sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__driver", "sqlserver")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__host", sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__database", db.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__username", "sa")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo__password", sqlserver.Resource.PasswordParameter.Value)
// Configure connection to the master database for server-level operations
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__name", "SqlMaster")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__driver", "sqlserver")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__host", sqlserver.Resource.Name)
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__database", "master")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__username", "sa")
.WithEnvironment("SQLPAD_CONNECTIONS__sqlserverdemo1__password", sqlserver.Resource.PasswordParameter.Value)
// Ensure SQLPad starts after the database and server are ready
.WithParentRelationship(db)
.WaitFor(db)
.WaitFor(sqlserver)
So please use SqlExtensionsAspire and tell me how it was.