Interface IMigrator
- Namespace
- Microsoft.EntityFrameworkCore.Migrations
- Assembly
- Microsoft.EntityFrameworkCore.Relational.dll
The main service used to generated an EF Core Migrations script or migrate a database directly.
public interface IMigrator
Remarks
The service lifetime is Scoped. This means that each Microsoft.EntityFrameworkCore.DbContext instance will use its own instance of this service. The implementation may depend on other services registered with any lifetime. The implementation does not need to be thread-safe.
See Database migrations for more information and examples.
Methods
GenerateScript(string?, string?, MigrationsSqlGenerationOptions)
Generates a SQL script to migrate a database either in its entirety, or starting and ending at specified migrations.
string GenerateScript(string? fromMigration = null, string? toMigration = null, MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default)
Parameters
fromMigration
stringThe migration to start from, or null to start from the empty database.
toMigration
stringThe target migration to migrate the database to, or null to migrate to the latest.
options
MigrationsSqlGenerationOptionsThe options to use when generating SQL for migrations.
Returns
- string
The generated script.
Remarks
See Database migrations for more information and examples.
Migrate(string?)
Migrates the database to either a specified target migration or up to the latest migration that exists in the IMigrationsAssembly.
void Migrate(string? targetMigration = null)
Parameters
targetMigration
stringThe target migration to migrate the database to, or null to migrate to the latest.
Remarks
See Database migrations for more information and examples.
MigrateAsync(string?, CancellationToken)
Migrates the database to either a specified target migration or up to the latest migration that exists in the IMigrationsAssembly.
Task MigrateAsync(string? targetMigration = null, CancellationToken cancellationToken = default)
Parameters
targetMigration
stringThe target migration to migrate the database to, or null to migrate to the latest.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation
Remarks
See Database migrations for more information and examples.
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.