### Configure DbContext using OnConfiguring Source: https://www.npgsql.org/efcore/index.html Use the OnConfiguring method to set up your DbContext with the Npgsql provider. This is suitable for simple scenarios but discouraged for production. ```csharp public class BloggingContext : DbContext { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(""); } // At the point where you need to perform a database operation: using var context = new BloggingContext(); // Use the context... ``` -------------------------------- ### Configure Project File for Npgsql EF Core Source: https://www.npgsql.org/efcore/index.html Add the Npgsql.EntityFrameworkCore.PostgreSQL package to your .csproj file to use the provider. ```xml net8.0 ``` -------------------------------- ### Scaffold EF Core Model from Existing Database Source: https://www.npgsql.org/efcore/index.html Use the dotnet CLI to reverse-engineer a code model from an existing PostgreSQL database for use with the Npgsql EF Core provider. ```bash dotnet ef dbcontext scaffold "Host=my_host;Database=my_db;Username=my_user;Password=my_pw" Npgsql.EntityFrameworkCore.PostgreSQL ``` -------------------------------- ### Configure DbContext using PooledDbContextFactory Source: https://www.npgsql.org/efcore/index.html Utilize PooledDbContextFactory for efficient DbContext creation and management, especially in performance-sensitive applications. ```csharp var dbContextFactory = new PooledDbContextFactory( new DbContextOptionsBuilder() .UseNpgsql("") .Options); // At the point where you need to perform a database operation: using var context = dbContextFactory.CreateDbContext(); // Use the context... ``` -------------------------------- ### Configure DbContext with Dependency Injection in ASP.NET Source: https://www.npgsql.org/efcore/index.html Configure the Npgsql EF Core provider within an ASP.NET application's dependency injection container using AddDbContextPool. ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContextPool(opt => opt.UseNpgsql(builder.Configuration.GetConnectionString("BloggingContext"))); public class BloggingContext(DbContextOptions options) : DbContext(options) { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } } ``` -------------------------------- ### Configure Npgsql Provider Options with PostgreSQL Version and NodaTime Source: https://www.npgsql.org/efcore/index.html Configure specific Npgsql provider options, including PostgreSQL version and NodaTime integration, directly within the UseNpgsql call. ```csharp builder.Services.AddDbContextPool(opt => opt.UseNpgsql( builder.Configuration.GetConnectionString("BloggingContext"), o => o .SetPostgresVersion(13, 0) .UseNodaTime() .MapEnum("mood"))); ``` -------------------------------- ### Configure Npgsql DataSource with Client Certificate Source: https://www.npgsql.org/efcore/index.html Configure lower-level Npgsql ADO.NET provider settings, such as using a client certificate, via the ConfigureDataSource method. ```csharp builder.Services.AddDbContextPool(opt => opt.UseNpgsql( builder.Configuration.GetConnectionString("BloggingContext"), o => o.ConfigureDataSource(dataSourceBuilder => dataSourceBuilder.UseClientCertificate(certificate)))); ``` -------------------------------- ### Define EF Core Models for Blog and Post Source: https://www.npgsql.org/efcore/index.html Define .NET classes to represent your database entities, such as Blog and Post, including their properties and relationships. ```csharp public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } ``` -------------------------------- ### Configure NpgsqlDataSource Externally for EF Core Source: https://www.npgsql.org/efcore/index.html When using EF Core versions prior to 9.0, create an NpgsqlDataSource externally and pass it to UseNpgsql. Ensure mappings and plugins are configured at both the NpgsqlDataSource and EF provider levels. ```csharp var dataSourceBuilder = new NpgsqlDataSourceBuilder(builder.Configuration.GetConnectionString("BloggingContext")); dataSourceBuilder.MapEnum(); dataSourceBuilder.UseNodaTime(); var dataSource = dataSourceBuilder.Build(); builder.Services.AddDbContextPool(opt => opt .UseNpgsql(dataSource, o => o .SetPostgresVersion(13, 0) .UseNodaTime() .MapEnum("mood"))); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.