Skip to content

Commit

Permalink
fix(db): use design time context factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenrikur committed Aug 4, 2024
1 parent ed27ca2 commit b4053fd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN dotnet restore \
&& dotnet publish src/Eurofurence.App.Server.Web/Eurofurence.App.Server.Web.csproj --output "/app/artifacts" --configuration Release
RUN dotnet tool install --global dotnet-ef \
&& export PATH="$PATH:/root/.dotnet/tools" \
&& export ASPNETCORE_ENVIRONMENT="sample" \
&& dotnet ef migrations bundle -o "/app/artifacts/db-migration-bundle" -p src/Eurofurence.App.Infrastructure.EntityFramework
ENTRYPOINT dotnet artifacts/Eurofurence.App.Server.Web.dll http://*:30001
EXPOSE 30001
Expand Down
21 changes: 0 additions & 21 deletions src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace Eurofurence.App.Infrastructure.EntityFramework
{
public class AppDbContext : DbContext
{
public AppDbContext() { }
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
Expand Down Expand Up @@ -66,26 +65,6 @@ public AppDbContext(DbContextOptions<AppDbContext> options)
public virtual DbSet<TopicRecord> Topics { get; set; }
public virtual DbSet<LinkFragment> LinkFragments { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var connectionString = "Server=db; Port=3306; Database=ef_backend; user=root; SslMode=Preferred;";

var serverVersionString = Environment.GetEnvironmentVariable("MYSQL_VERSION");
ServerVersion serverVersion;
if (string.IsNullOrEmpty(serverVersionString) || !ServerVersion.TryParse(serverVersionString, out serverVersion))
{
serverVersion = ServerVersion.AutoDetect(connectionString);
}

optionsBuilder.UseMySql(
connectionString,
serverVersion,
mySqlOptions => mySqlOptions.UseMicrosoftJson());
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using Eurofurence.App.Infrastructure.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
// Get environment
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

// Build config
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.Build();

// Get connection string
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
var connectionString = config.GetConnectionString("Eurofurence");

var serverVersionString = Environment.GetEnvironmentVariable("MYSQL_VERSION");
ServerVersion serverVersion;
if (string.IsNullOrEmpty(serverVersionString) || !ServerVersion.TryParse(serverVersionString, out serverVersion))
{
serverVersion = ServerVersion.AutoDetect(connectionString);
}

optionsBuilder.UseMySql(
connectionString,
serverVersion,
mySqlOptions => mySqlOptions.UseMicrosoftJson());
return new AppDbContext(optionsBuilder.Options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Json.Microsoft" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Eurofurence.App.Domain.Model\Eurofurence.App.Domain.Model.csproj" />
Expand Down

0 comments on commit b4053fd

Please sign in to comment.