Share via


DatabaseFacade.EnsureCreatedAsync(CancellationToken) Metodo

Definizione

Assicura che il database per il contesto esista.

public virtual System.Threading.Tasks.Task<bool> EnsureCreatedAsync (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> EnsureCreatedAsync (System.Threading.CancellationToken cancellationToken = default);
abstract member EnsureCreatedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
override this.EnsureCreatedAsync : 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 EnsureCreatedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
override this.EnsureCreatedAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Overridable Function EnsureCreatedAsync (Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)

Parametri

cancellationToken
CancellationToken

Oggetto CancellationToken da osservare durante l'attesa del completamento dell'attività.

Restituisce

Attività che rappresenta l'operazione di salvataggio asincrono. Il risultato dell'attività contiene true se il database viene creato, false se esiste già.

Attributi

Eccezioni

Commenti

  • Se il database esiste e include tabelle, non viene eseguita alcuna azione. Non viene fatto nulla per assicurarsi che lo schema del database sia compatibile con il modello Entity Framework.
  • Se il database esiste ma non dispone di tabelle, viene usato il modello Entity Framework per creare lo schema del database.
  • Se il database non esiste, il database viene creato e il modello Entity Framework viene usato per creare lo schema del database.

È comune usare EnsureCreatedAsync(CancellationToken) immediatamente quando EnsureDeletedAsync(CancellationToken) si esegue il test o la prototipazione usando Entity Framework. Ciò garantisce che il database si trova in uno stato pulito prima di ogni esecuzione del test/prototipo. Si noti tuttavia che i dati nel database non sono mantenuti.

Si noti che questa API non usa le migrazioni per creare il database. Inoltre, il database creato non può essere aggiornato in un secondo momento usando le migrazioni. Se si usa un database relazionale e si usano le migrazioni, è possibile usare Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.MigrateAsync per assicurarsi che il database venga creato usando le migrazioni e che tutte le migrazioni siano state applicate.

Entity Framework Core non supporta l'esecuzione di più operazioni parallele nella stessa istanza di DbContext. Ciò include sia l'esecuzione parallela di query asincrone che qualsiasi uso simultaneo esplicito da più thread. Pertanto, attendere immediatamente le chiamate asincrone o usare istanze dbContext separate per le operazioni eseguite in parallelo. Per altre informazioni ed esempi, vedere Evitare problemi di threading DbContext .

Per altre informazioni ed esempi, vedere Gestione degli schemi di database con LE API di creazione di EF Core e database .

Si applica a