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