Share via


DatabaseFacade.EnsureCreated Method

Definition

Ensures that the database for the context exists.

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

Returns

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

Attributes

Remarks

  • If the database exists and has any tables, then no action is taken. Nothing is done to ensure the database schema is compatible with the Entity Framework model.
  • If the database exists but does not have any tables, then the Entity Framework model is used to create the database schema.
  • If the database does not exist, then the database is created and the Entity Framework model is used to create the database schema.

It is common to use EnsureCreated() immediately following EnsureDeleted() 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.

Note that this API does **not** use migrations to create the database. In addition, the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, then you can use Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate to ensure the database is created using migrations and that all migrations have been applied.

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

Applies to