File tree Expand file tree Collapse file tree
MigrationService/Initializers Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111 <ItemGroup >
1212 <PackageReference Include =" Aspire.Hosting.AppHost" Version =" 9.0.0-rc.1.24511.1" />
1313 <PackageReference Include =" Aspire.Hosting.SqlServer" Version =" 9.0.0-rc.1.24511.1" />
14+ <PackageReference Include =" Microsoft.EntityFrameworkCore.SqlServer" Version =" 9.0.0-rc.2.24474.1" />
1415 </ItemGroup >
1516
1617 <ItemGroup >
Original file line number Diff line number Diff line change 1+ using Microsoft . EntityFrameworkCore ;
2+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
3+
4+ namespace AppHost . Commands ;
5+
6+ public static class SqlServerCommandExt
7+ {
8+ public static IResourceBuilder < SqlServerServerResource > WithDropDatabaseCommand (
9+ this IResourceBuilder < SqlServerServerResource > builder )
10+ {
11+ builder . WithCommand (
12+ "drop-database" ,
13+ "Drop Database" ,
14+ async context =>
15+ {
16+ var connectionString = await builder . Resource . GetConnectionStringAsync ( ) ;
17+ ArgumentException . ThrowIfNullOrWhiteSpace ( connectionString ) ;
18+
19+ var optionsBuilder = new DbContextOptionsBuilder < DbContext > ( ) ;
20+ optionsBuilder . UseSqlServer ( connectionString ) ;
21+ var db = new DbContext ( optionsBuilder . Options ) ;
22+ await db . Database . EnsureDeletedAsync ( ) ;
23+
24+ return CommandResults . Success ( ) ;
25+ } ,
26+ context =>
27+ {
28+ if ( context . ResourceSnapshot . HealthStatus is HealthStatus . Healthy )
29+ {
30+ return ResourceCommandState . Enabled ;
31+ }
32+
33+ return ResourceCommandState . Disabled ;
34+ } ) ;
35+ return builder ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ using AppHost . Commands ;
12using Projects ;
23
34var builder = DistributedApplication . CreateBuilder ( args ) ;
45
56// Ensure the port doesn't conflict with other docker containers (or remove it altogether)
67var sqlServer = builder
78 . AddSqlServer ( "sql" , port : 1800 )
9+ . WithDropDatabaseCommand ( )
810 . WithLifetime ( ContainerLifetime . Persistent ) ;
911
1012var db = sqlServer
Original file line number Diff line number Diff line change @@ -21,10 +21,10 @@ public async Task EnsureDatabaseAsync(CancellationToken cancellationToken)
2121 var strategy = DbContext . Database . CreateExecutionStrategy ( ) ;
2222 await strategy . ExecuteAsync ( async ( ) =>
2323 {
24+ // Create the database if it does not exist.
25+ // Do this first so there is then a database to start a transaction against.
2426 if ( ! await dbCreator . ExistsAsync ( cancellationToken ) )
2527 {
26- // Create the database if it does not exist.
27- // Do this first so there is then a database to start a transaction against.
2828 await dbCreator . CreateAsync ( cancellationToken ) ;
2929 }
3030 } ) ;
You can’t perform that action at this time.
0 commit comments