管理資料庫結構描述Managing Database Schemas

EF Core 提供兩種主要方式來保持 EF Core 模型與資料庫結構描述同步。若要從兩者中進行選擇,請決定 EF Core 模型還是資料庫結構描述為真實來源。EF Core provides two primary ways of keeping your EF Core model and database schema in sync. To choose between the two, decide whether your EF Core model or the database schema is the source of truth.

如果您想要 EF Core 模型成為真實來源,請使用移轉If you want your EF Core model to be the source of truth, use Migrations. 當您變更 EF Core 模型時,這種方式會以遞增方式將對應的結構描述變更套用至資料庫;因此,它會與 EF Core 模型相容。As you make changes to your EF Core model, this approach incrementally applies the corresponding schema changes to your database so that it remains compatible with your EF Core model.

如果您想要資料庫結構描述成為真實來源,請使用反向工程Use Reverse Engineering if you want your database schema to be the source of truth. 此方式可讓您將資料庫結構描述反向工程到 EF Core 模型,以支援 DbContext 和實體類型類別。This approach allows you to scaffold a DbContext and the entity type classes by reverse engineering your database schema into an EF Core model.

注意

建立和捨棄 API 也可以從 EF Core 模型建立資料庫結構描述。The create and drop APIs can also create the database schema from your EF Core model. 不過,它們主要用於測試、原型設計,以及可接受捨棄資料庫的其他案例。However, they are primarily for testing, prototyping, and other scenarios where dropping the database is acceptable.