Skip to content

Commit

Permalink
feat: design time DbContext factory
Browse files Browse the repository at this point in the history
Co-authored-by: Steven He <[email protected]>
  • Loading branch information
GZTimeWalker and hez2010 committed Nov 19, 2024
1 parent 06d53b1 commit a4edc4a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/GZCTF/GZCTF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<WarningLevel>4</WarningLevel>
<ServerGarbageCollection>true</ServerGarbageCollection>
<EFOptimizeContext>true</EFOptimizeContext>
<EFPrecompileQueriesStage>disabled</EFPrecompileQueriesStage>
<EFPrecompileQueriesStage>none</EFPrecompileQueriesStage>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
</PropertyGroup>

Expand Down
15 changes: 15 additions & 0 deletions src/GZCTF/Models/DesignTimeAppDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;

namespace GZCTF.Models;

public class DesignTimeAppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseNpgsql(o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));

return new AppDbContext(builder.Options);
}
}
4 changes: 3 additions & 1 deletion src/GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
global using GZCTF.Utils;
global using AppDbContext = GZCTF.Models.AppDbContext;
global using TaskStatus = GZCTF.Utils.TaskStatus;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net.Mime;
using System.Reflection;
Expand All @@ -24,6 +25,7 @@
using GZCTF.Extensions;
using GZCTF.Hubs;
using GZCTF.Middlewares;
using GZCTF.Models;
using GZCTF.Models.Internal;
using GZCTF.Repositories;
using GZCTF.Repositories.Interface;
Expand All @@ -32,7 +34,6 @@
using GZCTF.Services.Config;
using GZCTF.Services.Container;
using GZCTF.Services.Mail;
using GZCTF.Utils;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -398,6 +399,7 @@ namespace GZCTF
{
public class Program
{
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(DesignTimeAppDbContextFactory))]
static Program()
{
using Stream stream = typeof(Program).Assembly
Expand Down

0 comments on commit a4edc4a

Please sign in to comment.