< Summary

Information
Class: Api.Database.SchemaModelCacheKeyFactory
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Database/PGContext.cs
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 46
Line coverage: 100%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Create(...)50%22100%

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
 14public partial class PGContext(DbContextOptions options, IConfiguration? config = null) : DbContext(options)
 15{
 16    public string SchemaName { get; set; } = config?["ConnectionStrings:PGSchema"] ?? "public";
 17
 18    protected override void OnModelCreating(ModelBuilder modelBuilder)
 19    {
 20        modelBuilder.HasDefaultSchema(SchemaName);
 21
 22        base.OnModelCreating(modelBuilder);
 23        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(this.GetType()!)!);
 24    }
 25
 26    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 27        => optionsBuilder
 28        .ReplaceService<IModelCacheKeyFactory, SchemaModelCacheKeyFactory>()
 29        .UseSnakeCaseNamingConvention()
 30        .UseValidationCheckConstraints()
 31        .UseNpgsql(
 32                config?.GetConnectionString("PG") ?? ""
 33                , nopts => nopts
 34                .SetPostgresVersion(18, 0)
 35                .UseNodaTime());
 36}
 37
 38public class SchemaModelCacheKeyFactory : IModelCacheKeyFactory
 39{
 40    public object Create(DbContext context, bool designTime)
 141    {
 142        return context is PGContext pgContext
 143            ? (context.GetType(), pgContext.SchemaName, designTime)
 144            : (context.GetType(), designTime);
 145    }
 46}