Class RelationalDatabaseCreator
- Namespace
- Microsoft.EntityFrameworkCore.Storage
- Assembly
- Microsoft.EntityFrameworkCore.Relational.dll
Performs database/schema creation, and other related operations.
This type is typically used by database providers (and other extensions). It is generally not used in application code.
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.
public abstract class RelationalDatabaseCreator : IRelationalDatabaseCreator, IDatabaseCreator
- Inheritance
-
RelationalDatabaseCreator
- Implements
-
IDatabaseCreator
- Inherited Members
Constructors
RelationalDatabaseCreator(RelationalDatabaseCreatorDependencies)
Initializes a new instance of the RelationalDatabaseCreator class.
protected RelationalDatabaseCreator(RelationalDatabaseCreatorDependencies dependencies)
Parameters
dependencies
RelationalDatabaseCreatorDependenciesParameter object containing dependencies for this service.
Properties
Dependencies
Parameter object containing service dependencies.
protected virtual RelationalDatabaseCreatorDependencies Dependencies { get; }
Property Value
Methods
CanConnect()
Determines whether or not the database is available and can be connected to.
Any exceptions thrown when attempting to connect are caught and not propagated to the application.
The configured connection string is used to create the connection in the normal way, so all configured options such as timeouts are honored.
Note that being able to connect to the database does not mean that it is up-to-date with regard to schema creation, etc.
public virtual bool CanConnect()
Returns
CanConnectAsync(CancellationToken)
Determines whether or not the database is available and can be connected to.
Any exceptions thrown when attempting to connect are caught and not propagated to the application.
The configured connection string is used to create the connection in the normal way, so all configured options such as timeouts are honored.
Note that being able to connect to the database does not mean that it is up-to-date with regard to schema creation, etc.
public virtual Task<bool> CanConnectAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Create()
Creates the physical database. Does not attempt to populate it with any schema.
public abstract void Create()
CreateAsync(CancellationToken)
Asynchronously creates the physical database. Does not attempt to populate it with any schema.
public virtual Task CreateAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
CreateTables()
Creates all tables for the current model in the database. No attempt is made to incrementally update the schema. It is assumed that none of the tables exist in the database.
public virtual void CreateTables()
CreateTablesAsync(CancellationToken)
Asynchronously creates all tables for the current model in the database. No attempt is made to incrementally update the schema. It is assumed that none of the tables exist in the database.
public virtual Task CreateTablesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
Delete()
Deletes the physical database.
public abstract void Delete()
DeleteAsync(CancellationToken)
Asynchronously deletes the physical database.
public virtual Task DeleteAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
EnsureCreated()
Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.
public virtual bool EnsureCreated()
Returns
EnsureCreatedAsync(CancellationToken)
Asynchronously ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.
public virtual Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous save operation. The task result contains true if the database is created, false if it already existed.
EnsureDeleted()
Ensures that the database for the context does not exist. If it does not exist, no action is taken. If it does exist then the database is deleted.
Warning: The entire database is deleted an no effort is made to remove just the database objects that are used by the model for this context.
public virtual bool EnsureDeleted()
Returns
EnsureDeletedAsync(CancellationToken)
Asynchronously ensures that the database for the context does not exist. If it does not exist, no action is taken. If it does exist then the database is deleted.
Warning: The entire database is deleted an no effort is made to remove just the database objects that are used by the model for this context.
public virtual Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous save operation. The task result contains true if the database is deleted, false if it did not exist.
Exists()
Determines whether the physical database exists. No attempt is made to determine if the database contains the schema for the current model.
public abstract bool Exists()
Returns
ExistsAsync(CancellationToken)
Asynchronously determines whether the physical database exists. No attempt is made to determine if the database contains the schema for the current model.
public virtual Task<bool> ExistsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains true if the database exists; otherwise false.
GenerateCreateScript()
Generates a script to create all tables for the current model.
public virtual string GenerateCreateScript()
Returns
- string
A SQL script.
GetCreateTablesCommands(MigrationsSqlGenerationOptions)
Gets the commands that will create all tables from the model.
protected virtual IReadOnlyList<MigrationCommand> GetCreateTablesCommands(MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default)
Parameters
options
MigrationsSqlGenerationOptionsThe options to use when generating commands.
Returns
- IReadOnlyList<MigrationCommand>
The generated commands.
HasTables()
Determines whether the database contains any tables. No attempt is made to determine if tables belong to the current model or not.
public abstract bool HasTables()
Returns
- bool
A value indicating whether any tables are present in the database.
HasTablesAsync(CancellationToken)
Asynchronously determines whether the database contains any tables. No attempt is made to determine if tables belong to the current model or not.
public virtual Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.