Share via


DatabaseFacade.EnsureDeletedAsync(CancellationToken) Method

Definition

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, and no effort is made to remove just the database objects that are used by the model for this context.

public virtual System.Threading.Tasks.Task<bool> EnsureDeletedAsync (System.Threading.CancellationToken cancellationToken = default);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Migrations operations require building the design-time model which is not supported with NativeAOT Use a migration bundle or an alternate way of executing migration operations.")]
public virtual System.Threading.Tasks.Task<bool> EnsureDeletedAsync (System.Threading.CancellationToken cancellationToken = default);
abstract member EnsureDeletedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
override this.EnsureDeletedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Migrations operations require building the design-time model which is not supported with NativeAOT Use a migration bundle or an alternate way of executing migration operations.")>]
abstract member EnsureDeletedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
override this.EnsureDeletedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Overridable Function EnsureDeletedAsync (Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)

Parameters

cancellationToken
CancellationToken

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

Returns

A task that represents the asynchronous save operation. The task result contains true if the database is deleted, false if it did not exist.

Attributes

Exceptions

Remarks

It is common to use EnsureCreatedAsync(CancellationToken) immediately following EnsureDeletedAsync(CancellationToken) when testing or prototyping using Entity Framework. This ensures that the database is in a clean state before each execution of the test/prototype. Note, however, that data in the database is not preserved.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Managing database schemas with EF Core and Database creation APIs for more information and examples.

Applies to