Skip to content

Commit

Permalink
Refactor database initialization and update project references
Browse files Browse the repository at this point in the history
Removed specific package references from DatabaseProject.csproj and switched the project reference to Entities.csproj. Database initialization has been refactored to a separate method in Initializer.cs and this method is now called in Program.cs. FrameworkReference to Microsoft.AspNetCore.App was added to Database.csproj. Some changes were also made to how model configuration is handled.
  • Loading branch information
Gary Woodfine committed Jan 18, 2024
1 parent 2c334d0 commit 6981f3a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 20 deletions.
12 changes: 1 addition & 11 deletions src/Database/DatabaseProject/DatabaseProject.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Threenine.Database.Configuration" Version="1.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Models/Models.csproj" />
<ProjectReference Include="../Entities/Entities.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/ModelConfiguration/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
},
"sourceName": "ModelConfiguration",
"symbols": {
"root": {
"type": "parameter",
"replaces": "placeHolder",
"dataType": "string",
"defaultValue": "Threenine"
},
"model": {
"type": "parameter",
"replaces": "Model",
Expand Down
1 change: 1 addition & 0 deletions src/ModelConfiguration/ModelConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Threenine;
#if(useMSSQL)
using Threenine.Configurations.SqlServer;
#endif
Expand Down
16 changes: 8 additions & 8 deletions src/Solution/src/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@

var app = builder.Build();


app.UseSerilogRequestLogging();
app.UseMiddleware<ExceptionHandlingMiddleware>();

// Database migrations
using (var serviceScope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApiSolutionContext>();
context?.Database.Migrate();
}
app.DatabaseInitialise();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api v1"));
app.UseSwaggerUI(settings =>
{
settings.SwaggerEndpoint("/swagger/v1/swagger.json", "Api v1");
});


}
app.UseHttpsRedirection();

Expand Down
6 changes: 5 additions & 1 deletion src/Solution/src/Database/Database/Database.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Entities\Entities.csproj" />
</ItemGroup>



</Project>
4 changes: 4 additions & 0 deletions src/Solution/src/Database/Database/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))"/>
<ItemGroup>
<Using Include="placeHolder.Database.Entities" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Solution/src/Database/Database/Initializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;


namespace Database.ApiSolutions
{
public static class Initializer
{
public static void DatabaseInitialise(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetService<ApiSolutionContext>();
context.Database.Migrate();
}
}
}

0 comments on commit 6981f3a

Please sign in to comment.