Table of Contents

Interface IDbContextFactory<TContext>

Namespace
Microsoft.EntityFrameworkCore
Assembly
Microsoft.EntityFrameworkCore.dll

Defines a factory for creating DbContext instances.

public interface IDbContextFactory<TContext> where TContext : DbContext

Type Parameters

TContext

The DbContext type to create.

Remarks

See Using DbContextFactory for more information and examples.

Methods

CreateDbContext()

Creates a new DbContext instance.

TContext CreateDbContext()

Returns

TContext

A new context instance.

Remarks

The caller is responsible for disposing the context; it will not be disposed by any dependency injection container.

CreateDbContextAsync(CancellationToken)

Creates a new DbContext instance in an async context.

Task<TContext> CreateDbContextAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task<TContext>

A task containing the created context that represents the asynchronous operation.

Remarks

The caller is responsible for disposing the context; it will not be disposed by any dependency injection container.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.