Table of Contents

Interface IDatabaseCreator

Namespace
Microsoft.EntityFrameworkCore.Storage
Assembly
Microsoft.EntityFrameworkCore.dll

Creates and deletes databases for a given database provider.

This interface is typically used by database providers (and other extensions). It is generally not used in application code.

public interface IDatabaseCreator

Remarks

The service lifetime is Scoped. This means that each 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 Implementation of database providers and extensions for more information and examples.

Methods

CanConnect()

Determines whether or not the database is available and can be connected to.

bool CanConnect()

Returns

bool

true if the database is available; false otherwise.

Remarks

Note that being able to connect to the database does not mean that it is up-to-date with regard to schema creation, etc.

CanConnectAsync(CancellationToken)

Determines whether or not the database is available and can be connected to.

Task<bool> CanConnectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

Task<bool>

true if the database is available; false otherwise.

Remarks

Note that being able to connect to the database does not mean that it is up-to-date with regard to schema creation, etc.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

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.

bool EnsureCreated()

Returns

bool

true if the database is created, false if it already existed.

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.

Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A 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.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

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.

bool EnsureDeleted()

Returns

bool

true if the database is deleted, false if it did not exist.

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.

Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A 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.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.