Search Results for

    Show / Hide Table of Contents

    Class NpgsqlServiceCollectionExtensions

    Provides extension methods to configure Entity Framework Core for Npgsql.

    Inheritance
    object
    NpgsqlServiceCollectionExtensions
    Namespace: Microsoft.Extensions.DependencyInjection
    Assembly: Npgsql.EntityFrameworkCore.PostgreSQL.dll
    Syntax
    public static class NpgsqlServiceCollectionExtensions

    Methods

    | Improve this Doc View Source

    AddEntityFrameworkNpgsql(IServiceCollection)

    Adds the services required by the Npgsql database provider for Entity Framework to an .

    Calling this method is no longer necessary when building most applications, including those that use dependency injection in ASP.NET or elsewhere. It is only needed when building the internal service provider for use with the method. This is not recommend other than for some advanced scenarios.

    Declaration
    public static IServiceCollection AddEntityFrameworkNpgsql(this IServiceCollection serviceCollection)
    Parameters
    Type Name Description
    IServiceCollection serviceCollection

    The to add services to.

    Returns
    Type Description
    IServiceCollection

    The same service collection so that multiple calls can be chained.

    | Improve this Doc View Source

    AddNpgsql<TContext>(IServiceCollection, string?, Action<NpgsqlDbContextOptionsBuilder>?, Action<DbContextOptionsBuilder>?)

    Registers the given Entity Framework context as a service in the and configures it to connect to a PostgreSQL database.

    Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating instances directly with its constructor. The method can then be overridden to configure the SQL Server provider and connection string.

    To configure the for the context, either override the method in your derived context, or supply an optional action to configure the for the context.

    For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

    Declaration
    public static IServiceCollection AddNpgsql<TContext>(this IServiceCollection serviceCollection, string? connectionString, Action<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = null, Action<DbContextOptionsBuilder>? optionsAction = null) where TContext : DbContext
    Parameters
    Type Name Description
    IServiceCollection serviceCollection

    The to add services to.

    string? connectionString

    The connection string of the database to connect to.

    Action<><NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction

    An optional action to allow additional SQL Server specific configuration.

    Action<><DbContextOptionsBuilder>? optionsAction

    An optional action to configure the for the context.

    Returns
    Type Description
    IServiceCollection

    The same service collection so that multiple calls can be chained.

    Type Parameters
    Name Description
    TContext

    The type of context to be registered.

    • Improve this Doc
    • View Source
    In This Article
    Back to top © Copyright 2023 The Npgsql Development Team