< Summary

Information
Class: Api.Database.PGContext
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Database/PGContext.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 46
Line coverage: 100%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
OnModelCreating(...)100%11100%
OnConfiguring(...)66.67%66100%

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Database/PGContext.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5using System.Reflection;
 6
 7using Microsoft.EntityFrameworkCore;
 8using Microsoft.EntityFrameworkCore.Infrastructure;
 9
 10using Npgsql;
 11
 12namespace Api.Database;
 13
 114public partial class PGContext(DbContextOptions options, IConfiguration? config = null) : DbContext(options)
 15{
 116    public string SchemaName { get; set; } = config?["ConnectionStrings:PGSchema"] ?? "public";
 17
 18    protected override void OnModelCreating(ModelBuilder modelBuilder)
 119    {
 120        modelBuilder.HasDefaultSchema(SchemaName);
 21
 122        base.OnModelCreating(modelBuilder);
 123        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(this.GetType()!)!);
 124    }
 25
 26    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 127        => optionsBuilder
 128        .ReplaceService<IModelCacheKeyFactory, SchemaModelCacheKeyFactory>()
 129        .UseSnakeCaseNamingConvention()
 130        .UseValidationCheckConstraints()
 131        .UseNpgsql(
 132                config?.GetConnectionString("PG") ?? ""
 133                , nopts => nopts
 134                .SetPostgresVersion(18, 0)
 135                .UseNodaTime());
 36}
 37
 38public class SchemaModelCacheKeyFactory : IModelCacheKeyFactory
 39{
 40    public object Create(DbContext context, bool designTime)
 41    {
 42        return context is PGContext pgContext
 43            ? (context.GetType(), pgContext.SchemaName, designTime)
 44            : (context.GetType(), designTime);
 45    }
 46}