【发布时间】:2019-11-05 18:00:07
【问题描述】:
使用最新的 ASP.NET Core 3 和 EF Core 3,我想像在以前版本的 EF 中所做的那样播种数据。我注意到在 Microsoft 文档中,他们将此代码作为如何播种的示例。
它使用如下代码更新迁移:
migrationBuilder.InsertData(
table: "Posts",
columns: new[] { "PostId", "BlogId", "Content", "Title", "AuthorName_First", "AuthorName_Last" },
values: new object[] { 1, 1, "Test 1", "First post", "Andriy", "Svyryd" });
migrationBuilder.InsertData(
table: "Posts",
columns: new[] { "PostId", "BlogId", "Content", "Title", "AuthorName_First", "AuthorName_Last" },
values: new object[] { 2, 1, "Test 2", "Second post", "Diego", "Vega" });
这对我来说似乎“不舒服”,因为我学会了初始化所有数据和表的方法是删除我的迁移文件夹,然后重新创建数据库。如果我手动更新迁移,那么我将永远保持此迁移。
是否有更好的方法来处理 EF Core 3 中的种子数据?也许使用 dbContext 或以某种方式在模型类本身中添加一些东西?
【问题讨论】:
标签: c# .net-core entity-framework-core asp-net-core-spa-services