我在创建迁移时遇到了问题,未创建迁移
当我在包中创建迁移时
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 中进行了更改
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
DbInitializer.Initialize(conetxt);
Package manager consol > Add-Migration FirstMigration and After Completion Update-Database
Update-Database 命令出现问题
最佳答案
使用 EF Core,您现在拥有 2 个不同的命令行工具, dotnet cli 和 PM控制台 .您可以查看 official docs以供进一步引用。
使用 dotnet CLI 时迁移可以添加:
Usage: dotnet ef migrations [options] [command]
Options:
- -h|--help => Show help information
- -v|--verbose => Enable verbose output
Commands:
- add => Add a new migration
- list => List the migrations
- remove => Remove the last migration
- script => Generate a SQL script from migrations
因此,为了添加新的 FirstMigration 迁移,您将运行:
>dotnet ef migrations add FirstMigration
相同的命令也可用于 Package Manager Console :
Install-Package Microsoft.EntityFrameworkCore.Tools -Pre
安装它们. (查看 the documentation 上关于并排安装 EF Core 和 EF6 命令的部分)为了添加迁移,您将运行:
>Add-Migration FirstMigration
关于asp.net-core-mvc - 如何在asp.net core 1.0中添加迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42605495/