From b4053fd07b351ad827354f5e707f69344b17969f Mon Sep 17 00:00:00 2001 From: Fenrikur <3359222+Fenrikur@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:15:37 +0200 Subject: [PATCH] fix(db): use design time context factory --- Dockerfile | 1 + .../AppDbContext.cs | 21 ---------- .../AppDbContextFactory.cs | 38 +++++++++++++++++++ ....App.Infrastructure.EntityFramework.csproj | 3 ++ 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContextFactory.cs diff --git a/Dockerfile b/Dockerfile index a512505e..ce160529 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContext.cs b/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContext.cs index 6bd9bb18..ca18e9b4 100644 --- a/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContext.cs +++ b/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContext.cs @@ -27,7 +27,6 @@ namespace Eurofurence.App.Infrastructure.EntityFramework { public class AppDbContext : DbContext { - public AppDbContext() { } public AppDbContext(DbContextOptions options) : base(options) { @@ -66,26 +65,6 @@ public AppDbContext(DbContextOptions options) public virtual DbSet Topics { get; set; } public virtual DbSet 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); diff --git a/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContextFactory.cs b/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContextFactory.cs new file mode 100644 index 00000000..30a2b780 --- /dev/null +++ b/src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContextFactory.cs @@ -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 +{ + 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(); + 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); + } +} \ No newline at end of file diff --git a/src/Eurofurence.App.Infrastructure.EntityFramework/Eurofurence.App.Infrastructure.EntityFramework.csproj b/src/Eurofurence.App.Infrastructure.EntityFramework/Eurofurence.App.Infrastructure.EntityFramework.csproj index 215effac..8502efa6 100644 --- a/src/Eurofurence.App.Infrastructure.EntityFramework/Eurofurence.App.Infrastructure.EntityFramework.csproj +++ b/src/Eurofurence.App.Infrastructure.EntityFramework/Eurofurence.App.Infrastructure.EntityFramework.csproj @@ -16,6 +16,9 @@ + + +