Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions TestHosts/TestHosts/Database/PataPawa/PataPawaContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Shared.General;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace TestHosts.Database.PataPawa
{
Expand Down Expand Up @@ -59,5 +61,59 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) {

base.OnModelCreating(modelBuilder);
}

private async Task SetDbInSimpleMode(CancellationToken cancellationToken)
{
var dbName = this.Database.GetDbConnection().Database;

var connection = this.Database.GetDbConnection();
if (connection.State != System.Data.ConnectionState.Open)
await connection.OpenAsync(cancellationToken);

// 1. Check current recovery model
await using var checkCommand = connection.CreateCommand();
checkCommand.CommandText = @"
SELECT recovery_model_desc
FROM sys.databases
WHERE name = @dbName;
";
var param = checkCommand.CreateParameter();
param.ParameterName = "@dbName";
param.Value = dbName;
checkCommand.Parameters.Add(param);

var result = await checkCommand.ExecuteScalarAsync(cancellationToken);
var currentRecoveryModel = result?.ToString();

if (currentRecoveryModel != "SIMPLE")
{
// 2. Alter database outside transaction
await using var alterCommand = connection.CreateCommand();
alterCommand.CommandText = $@"
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE [{dbName}] SET RECOVERY SIMPLE;
ALTER DATABASE [{dbName}] SET MULTI_USER;
";
// Execute outside EF transaction
await alterCommand.ExecuteNonQueryAsync(cancellationToken);
}
}

public virtual async Task MigrateAsync(CancellationToken cancellationToken)
{
if (this.Database.IsSqlServer())
{
try
{
await this.Database.MigrateAsync(cancellationToken);
await this.SetDbInSimpleMode(cancellationToken);
}
catch (Exception ex)
{
// Log the exception or handle it as needed
throw new InvalidOperationException("An error occurred while migrating the database.", ex);
}
}
}
}
}
38 changes: 38 additions & 0 deletions TestHosts/TestHosts/Database/TestBank/TestBankContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public virtual async Task MigrateAsync(CancellationToken cancellationToken)
try
{
await this.Database.MigrateAsync(cancellationToken);
await this.SetDbInSimpleMode(cancellationToken);
}
catch (Exception ex)
{
Expand All @@ -81,5 +82,42 @@ public virtual async Task MigrateAsync(CancellationToken cancellationToken)
}
}
}

private async Task SetDbInSimpleMode(CancellationToken cancellationToken)
{
var dbName = this.Database.GetDbConnection().Database;

var connection = this.Database.GetDbConnection();
if (connection.State != System.Data.ConnectionState.Open)
await connection.OpenAsync(cancellationToken);

// 1. Check current recovery model
await using var checkCommand = connection.CreateCommand();
checkCommand.CommandText = @"
SELECT recovery_model_desc
FROM sys.databases
WHERE name = @dbName;
";
var param = checkCommand.CreateParameter();
param.ParameterName = "@dbName";
param.Value = dbName;
checkCommand.Parameters.Add(param);

var result = await checkCommand.ExecuteScalarAsync(cancellationToken);
var currentRecoveryModel = result?.ToString();

if (currentRecoveryModel != "SIMPLE")
{
// 2. Alter database outside transaction
await using var alterCommand = connection.CreateCommand();
alterCommand.CommandText = $@"
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE [{dbName}] SET RECOVERY SIMPLE;
ALTER DATABASE [{dbName}] SET MULTI_USER;
";
// Execute outside EF transaction
await alterCommand.ExecuteNonQueryAsync(cancellationToken);
}
}
}
}
3 changes: 1 addition & 2 deletions TestHosts/TestHosts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@

// Custom middleware
app.UseMiddleware<TenantMiddleware>();
app.AddRequestLogging();
app.AddResponseLogging();
app.AddRequestResponseLogging();
app.AddExceptionHandler();

app.UseRouting();
Expand Down
4 changes: 2 additions & 2 deletions TestHosts/TestHosts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ public async Task StartAsync(CancellationToken cancellationToken)

if (pataPawaContext.Database.IsRelational()) {
// Example: apply migrations or seed data
pataPawaContext.Database.Migrate();
await pataPawaContext.Database.MigrateAsync(cancellationToken);
}
//else {
// await pataPawaContext.Database.EnsureCreatedAsync(cancellationToken);
//}

TestBankContext bankContext = scope.ServiceProvider.GetRequiredService<TestBankContext>();
if (bankContext.Database.IsRelational()) {
bankContext.Database.Migrate();
await bankContext.Database.MigrateAsync(cancellationToken);
}
//else {
// await bankContext.Database.EnsureCreatedAsync(cancellationToken);
Expand Down
20 changes: 10 additions & 10 deletions TestHosts/TestHosts/TestHosts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="9.0.0" />
<PackageReference Include="CoreWCF.Http" Version="1.8.0" />
<PackageReference Include="CoreWCF.Primitives" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="10.0.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="6.1.1" />
<PackageReference Include="Shared" Version="2026.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="6.1.2" />
<PackageReference Include="Shared" Version="2026.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.5" />

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.3">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.3">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading