Share via

.NET 9 Aspire project: SQL Server login failed and database becomes unhealthy on restart

mrcode 91 Reputation points
2025-08-09T09:36:32.71+00:00

Hi,

I have a project running on .NET 9 using .NET Aspire. When I start the project for the first time, everything works fine. But if I stop and start it again, I see the following error in the Aspire logs related to the database:

Login failed for user 'sa'. Reason: Password did not match that for the login provided.

Also, the database and server status show as Unhealthy.

Here is the relevant part of my Program.cs file in the Aspire project:

using Projects;

var builder = DistributedApplication.CreateBuilder(args);

var db = builder

.AddSqlServer("sql")

.WithLifetime(ContainerLifetime.Persistent)

.WithDataVolume(name: "MyDb", isReadOnly: false)

.AddDatabase("MyDb");

builder.AddProject<AbsenceFlow_Web>("web")

.WithReference(db).WaitFor(db);

builder.Build().Run();

If I go into Docker Desktop and delete everything related to the SQL database, then start the project again through Aspire, it works - but all my data is lost.

What could be causing this issue? How can I keep my database persistent without losing data or getting the login error?

Thanks in advance for any help!

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Harris 0 Reputation points
    2026-01-30T16:22:11.3066667+00:00

    The issue occurs because you haven't specified a password when using .WithDataVolume()

    The Aspire SQL integration docs mention that the password is stored within the data volume, and if the password changes (which it will in the above - because when one isn't specified the password is randomly generated).

    If you specify a password then as long as the password doesn't change it should continue to work

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.