From 1029db45425dacfc0290a58c6e3070d599ddec31 Mon Sep 17 00:00:00 2001 From: Bervianto Leo Pratama Date: Fri, 24 Apr 2026 09:11:38 +0700 Subject: [PATCH 1/4] Configure DbContext for migrations Added configuration for DbContext to handle migrations and set a default connection string. --- .../EntityFramework/BoilerplateDbContext.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs b/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs index 9e262869..abff44b3 100644 --- a/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs +++ b/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs @@ -34,6 +34,22 @@ public BoilerplateDbContext(DbContextOptions options) : base(options) } + /// + /// Configure DbContext for migrations + /// + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__BoilerplateConnectionString"); + if (string.IsNullOrEmpty(connectionString)) + { + connectionString = "Host=localhost;Database=testdb;Username=postgres;Password=postgres"; + } + optionsBuilder.UseNpgsql(connectionString); + } + } + /// /// Adding relationship /// From e1200ae3a49f864db4ce3697469ecb6937018814 Mon Sep 17 00:00:00 2001 From: Bervianto Leo Pratama Date: Fri, 24 Apr 2026 09:14:35 +0700 Subject: [PATCH 2/4] Add missing using directive for System namespace --- .../EntityFramework/BoilerplateDbContext.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs b/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs index abff44b3..c00fd3af 100644 --- a/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs +++ b/BervProject.WebApi.Boilerplate/EntityFramework/BoilerplateDbContext.cs @@ -1,4 +1,5 @@ -using BervProject.WebApi.Boilerplate.Entities; +using System; +using BervProject.WebApi.Boilerplate.Entities; using Microsoft.EntityFrameworkCore; namespace BervProject.WebApi.Boilerplate.EntityFramework From 61d52e81a0bdd3110ee921a7511acae8f47aa386 Mon Sep 17 00:00:00 2001 From: Bervianto Leo Pratama Date: Fri, 24 Apr 2026 09:23:11 +0700 Subject: [PATCH 3/4] Add DesignTimeDbContextFactory for EF Core --- .../Data/DesignTimeDbContextFactory.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs diff --git a/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs b/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs new file mode 100644 index 00000000..721fcd4e --- /dev/null +++ b/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs @@ -0,0 +1,22 @@ +using BervProject.WebApi.Boilerplate.EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; + +namespace BervProject.WebApi.Boilerplate.Data; + +public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory +{ + public BoilerplateDbContext CreateDbContext(string[] args) + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build(); + + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseNpgsql(configuration.GetConnectionString("BoilerplateConnectionString")); + + return new BoilerplateDbContext(optionsBuilder.Options); + } +} From c951d69ec923c5d14940c774375ad31d2c16228e Mon Sep 17 00:00:00 2001 From: Bervianto Leo Pratama Date: Fri, 24 Apr 2026 09:26:09 +0700 Subject: [PATCH 4/4] Update DesignTimeDbContextFactory.cs --- .../Data/DesignTimeDbContextFactory.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs b/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs index 721fcd4e..f79b4c91 100644 --- a/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs +++ b/BervProject.WebApi.Boilerplate/Data/DesignTimeDbContextFactory.cs @@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; +using System; +using System.IO; namespace BervProject.WebApi.Boilerplate.Data;