Class ServiceConfigurationExtensions
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
IServiceCollectionThe IServiceCollection to add services to.
configure
Func<IServiceProvider, DataOptions, DataOptions>An action to configure the DataOptions for the context.
lifetime
ServiceLifetimeThe 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
IServiceCollectionThe 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
ServiceLifetimeThe 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
IServiceCollectionThe 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
ServiceLifetimeThe 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 andTContext
and expose a constructor that takes DataContextOptions (where T isTContextImplementation
) 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.