< 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: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 49
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;
 6using LinqKit;
 7using Microsoft.EntityFrameworkCore;
 8using Microsoft.EntityFrameworkCore.Infrastructure;
 9using Npgsql;
 10
 11namespace Api.Database;
 12
 13public partial class PGContext(DbContextOptions options, IConfiguration? config = null)
 114    : DbContext(options)
 15{
 16    public string SchemaName { get; set; } =
 117        config?["ConnectionStrings:PGSchema"] ?? "public";
 18
 19    protected override void OnModelCreating(ModelBuilder modelBuilder)
 120    {
 121        modelBuilder.HasDefaultSchema(SchemaName);
 22
 123        base.OnModelCreating(modelBuilder);
 124        modelBuilder.ApplyConfigurationsFromAssembly(
 125            Assembly.GetAssembly(this.GetType()!)!
 126        );
 127    }
 28
 29    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
 130        optionsBuilder
 131            .ReplaceService<IModelCacheKeyFactory, SchemaModelCacheKeyFactory>()
 132            .UseSnakeCaseNamingConvention()
 133            .UseValidationCheckConstraints()
 134            .UseNpgsql(
 135                config?.GetConnectionString("PG") ?? "",
 136                nopts => nopts.SetPostgresVersion(18, 0).UseNodaTime()
 137            )
 138            .WithExpressionExpanding();
 39}
 40
 41public class SchemaModelCacheKeyFactory : IModelCacheKeyFactory
 42{
 43    public object Create(DbContext context, bool designTime)
 44    {
 45        return context is PGContext pgContext
 46            ? (context.GetType(), pgContext.SchemaName, designTime)
 47            : (context.GetType(), designTime);
 48    }
 49}