Lingua

DatabaseFacade.EnsureCreated Metodo

Definizione

Assicura che il database per il contesto esista.

public virtual bool EnsureCreated();
[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 bool EnsureCreated();
abstract member EnsureCreated : unit -> bool
override this.EnsureCreated : unit -> 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 EnsureCreated : unit -> bool
override this.EnsureCreated : unit -> bool
Public Overridable Function EnsureCreated () As Boolean

Valori restituiti

true se il database viene creato, false se esiste già.

Attributi

Commenti

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

È comune usare EnsureCreated() immediatamente dopo EnsureDeleted() il test o la creazione di prototipi con Entity Framework. In questo modo si garantisce che il database sia in uno stato pulito prima di ogni esecuzione del test/prototipo. Si noti, tuttavia, che i dati nel database non vengano mantenuti.

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

Per altre informazioni ed esempi, vedere Managing database schemas with EF Core and Database creation APIs (Gestione degli schemi di database con EF Core e API di creazione di database).

Si applica a