DatabaseFacade.EnsureCreated 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
確保上下文的資料庫存在。
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 進行測試或原型設計時,通常會緊接EnsureDeleted()著使用EnsureCreated()。 這確保資料庫在每次測試/原型執行前都處於乾淨狀態。 但請注意,資料庫中的資料並未被保存。
請注意,這個 API **不**使用遷移來建立資料庫。 此外,所建立的資料庫無法透過遷移功能進行後續更新。 如果你目標是關聯式資料庫並使用遷移,那麼你可以用來 Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate 確保資料庫是透過遷移建立的,且所有遷移都已套用。
更多資訊與範例請參閱「使用 EF Core 管理資料庫結構」及「資料庫建立 API」。