Table of Contents

Class ServiceConfigurationExtensions

Namespace
LinqToDB.AspNet
Assembly
linq2db.AspNet.dll
public static class ServiceConfigurationExtensions
Inheritance
ServiceConfigurationExtensions

Methods

AddLinqToDB(IServiceCollection, Func<IServiceProvider, DataOptions, DataOptions>, ServiceLifetime)

Registers DataConnection as the service IDataContext in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

public static IServiceCollection AddLinqToDB(this IServiceCollection serviceCollection, Func<IServiceProvider, DataOptions, DataOptions> configure, ServiceLifetime lifetime = ServiceLifetime.Scoped)

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

configure Func<IServiceProvider, DataOptions, DataOptions>

An action to configure the DataOptions for the context.

lifetime ServiceLifetime

The lifetime with which to register the Context service in the container. For one connection per request use Scoped (the default).

Returns

IServiceCollection

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddLinqToDB(options => options.UseSqlServer(connectionString));
}

Remarks

This will only work when you have 1 database connection across your whole application. If your application needs multiple different connections with different configurations then use AddLinqToDBContext<TContext>(IServiceCollection, Func<IServiceProvider, DataOptions, DataOptions>, ServiceLifetime) or AddLinqToDBContext<TContext, TContextImplementation>(IServiceCollection, Func<IServiceProvider, DataOptions, DataOptions>, ServiceLifetime).

To Resolve the connection inject IDataContext into your services.

AddLinqToDBContext<TContext>(IServiceCollection, Func<IServiceProvider, DataOptions, DataOptions>, ServiceLifetime)

Registers TContext as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

public static IServiceCollection AddLinqToDBContext<TContext>(this IServiceCollection serviceCollection, Func<IServiceProvider, DataOptions, DataOptions> configure, ServiceLifetime lifetime = ServiceLifetime.Scoped) where TContext : IDataContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

configure Func<IServiceProvider, DataOptions, DataOptions>

An action to configure the DataOptions for the context.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DataContextOptions and passes it to the base constructor of DataConnection.

lifetime ServiceLifetime

The lifetime with which to register the Context service in the container. For one connection per request use Scoped (the default).

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered. Must inherit from IDataContext and expose a constructor that takes DataContextOptions (where T is TContext) and passes it to the base constructor of DataConnection.

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddLinqToDBContext<MyContext>(options => options.UseSqlServer(connectionString));
}

Remarks

This method should be used when a custom context is required or when multiple contexts with different configurations are required.

AddLinqToDBContext<TContext, TContextImplementation>(IServiceCollection, Func<IServiceProvider, DataOptions, DataOptions>, ServiceLifetime)

Registers TContext as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

public static IServiceCollection AddLinqToDBContext<TContext, TContextImplementation>(this IServiceCollection serviceCollection, Func<IServiceProvider, DataOptions, DataOptions> configure, ServiceLifetime lifetime = ServiceLifetime.Scoped) where TContext : IDataContext where TContextImplementation : TContext, IDataContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

configure Func<IServiceProvider, DataOptions, DataOptions>

An action to configure the DataOptions for the context.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DataContextOptions and passes it to the base constructor of DataConnection.

lifetime ServiceLifetime

The lifetime with which to register the Context service in the container. For one connection per request use Scoped (the default).

Returns

IServiceCollection

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

Type Parameters

TContext

The class or interface that will be used to resolve the context from the container.

TContextImplementation

The concrete implementation type used to fulfill requests for TContext from the container. Must inherit from IDataContext and TContext and expose a constructor that takes DataContextOptions (where T is TContextImplementation) and passes it to the base constructor of DataConnection.

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddLinqToDBContext<IMyContext, MyContext>(options => options.UseSqlServer(connectionString));
}

Remarks

This method should be used when a custom context is required or when multiple contexts with different configurations are required.