我在创建迁移时遇到了问题,因此未创建迁移 当我在
中创建包中的迁移时DbInitializer
public static class DbInitializer
{
public static void Initialize(SchoolContext context)
{
context.Database.EnsureCreated();
// Look for any students.
if (context.Students.Any())
{
return; // DB has been seeded
}
var students = new Student[]
{
new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
};
foreach (Student s in students)
{
context.Students.Add(s);
}
context.SaveChanges();
}
}
Startup.cs中的MadeChanges
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
DbInitializer.Initialize(conetxt);
包管理器consol>添加迁移FirstMigration和完成后更新 - 数据库
Update-Database命令中出现问题
答案 0 :(得分:5)
使用EF Core,您现在有2种不同的命令行工具, dotnet cli 和 PM控制台。您可以查看official docs以获取进一步的参考。
使用dotnet CLI迁移时可以添加:
用法:dotnet ef migrations [options] [command]
选项:
- -h | --help =>显示帮助信息
- -v | --verbose =>启用详细输出
命令:
- add =>添加新迁移
- list =>列出迁移
- 删除=>删除上次迁移
- script =>从迁移生成SQL脚本
因此,为了添加新的 FirstMigration 迁移,您需要运行:
>dotnet ef migrations add FirstMigration
同样的命令也适用于Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools -Pre
安装它们。 (查看有关在the documentation)为了添加迁移,您将运行:
>Add-Migration FirstMigration