语言

DatabaseFacade.EnsureCreated 方法

定义

确保上下文的数据库存在。

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

返回

如果已创建数据库,则为 >。

特性

注解

  • 如果数据库存在且具有任何表,则不执行任何操作。 不执行任何操作,以确保数据库架构与 Entity Framework 模型兼容。
  • 如果数据库存在但没有任何表,则 Entity Framework 模型用于创建数据库架构。
  • 如果数据库不存在,则会创建数据库,并使用 Entity Framework 模型创建数据库架构。

使用 Entity Framework 进行测试或原型制作时,通常会立即EnsureDeleted()使用EnsureCreated()。 这可确保数据库在每次执行测试/原型之前处于干净状态。 但是,请注意,数据库中的数据不会保留。

请注意,此 API 不使用迁移来创建数据库。 此外,以后无法使用迁移更新创建的数据库。 如果以关系数据库为目标并使用迁移,则可以用于 Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate 确保数据库是使用迁移创建的,并且所有迁移都已应用。

有关详细信息和示例,请参阅 使用 EF Core 和 数据库创建 API 管理数据库 架构。

适用于