© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2w ago•
108 replies
Marco

.NET Core DBContext

beginnerasp.net
I am quickly learning .NET coming from previous experience in Java Spring and Python Flask, but I am finding some conflicting information online on integrating a Postgresql database in the ORM.

Firstly, my own code as of now:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace ChatAPI.ORM;

public class ChatDBContext : DbContext
{
    protected readonly IConfiguration _config;

    public ChatDBContext(IConfiguration config)
    {
        _config = config;
    }

    public ChatDBContext(DbContextOptions<ChatDBContext> options, 
                         IConfiguration config) : base(options)
    {
        _config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(
            _config.GetConnectionString("ChatAppDatabase")
                ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.")
            );
        
        using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
        ILogger logger = factory.CreateLogger<Program>();
        logger.LogInformation("Setup working");
    }
}
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace ChatAPI.ORM;

public class ChatDBContext : DbContext
{
    protected readonly IConfiguration _config;

    public ChatDBContext(IConfiguration config)
    {
        _config = config;
    }

    public ChatDBContext(DbContextOptions<ChatDBContext> options, 
                         IConfiguration config) : base(options)
    {
        _config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(
            _config.GetConnectionString("ChatAppDatabase")
                ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.")
            );
        
        using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
        ILogger logger = factory.CreateLogger<Program>();
        logger.LogInformation("Setup working");
    }
}


I very much prefer this separated syntax over simply adding a database connection with builder.Services.AddDbContext, but in all the examples I find using my syntax, I cannot find anything on how they actually trigger configuration! I added some logging to check if configuration even occurs, and it does not; it stays idle.

However, a function like
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
cannot be used, because I have literally written that function in the class!

This might simply be my lack of experience with C#, but after looking through a ton of different sources I am lost. How do I trigger configuration?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

✅ Transient DbContext vs DbContextFactory
C#CC# / help
2y ago
EF Core .NET 6 - Access DbContext from singleton service
C#CC# / help
4y ago
✅ ASP.NET Core DI: DbContext Not Shared in Scoped Services
C#CC# / help
7mo ago
ASP.NET Core - DbContext UOW with strict n-tier architecture
C#CC# / help
2y ago