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

傳回

true 如果資料庫已建立, false 則為 。

屬性

備註

  • 如果資料庫存在且具有任何資料表,則不會採取任何動作。 不執行任何動作,以確保資料庫架構與 Entity Framework 模型相容。
  • 如果資料庫存在但沒有任何資料表,則會使用 Entity Framework 模型來建立資料庫架構。
  • 如果資料庫不存在,則會建立資料庫,並使用 Entity Framework 模型來建立資料庫架構。

使用 Entity Framework 進行測試或原型設計時, EnsureDeleted() 通常會立即使用 EnsureCreated() 。 這可確保資料庫在每次執行測試/原型之前都處於乾淨狀態。 不過請注意,資料庫中的資料不會保留。

請注意,此 API 不會使用移轉來建立資料庫。 此外,稍後無法使用移轉來更新所建立的資料庫。 如果您的目標是關係資料庫並使用移轉,則可以使用 Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate 來確保資料庫是使用移轉建立的,而且已套用所有移轉。

如需詳細資訊和範例 ,請參閱使用 EF Core資料庫建立 API 管理資料庫 架構。

適用於