如何在 asp.net 核心 1.0 中添加迁移

How to Add Migration in asp.net core 1.0

我在创建迁移时遇到问题,未创建迁移 当我在

的包中创建迁移时

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

更新数据库命令出现问题

有了 EF Core,您现在有 2 个不同的命令行工具,dotnet cliPM 控制台。您可以查看 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

  • 如果你使用 Visual Studio 2017,它们应该已经安装好了
  • 如果您使用 Visual Studio 2017,则需要使用 Install-Package Microsoft.EntityFrameworkCore.Tools -Pre 安装它们。 (查看 the documentation 上并排安装 EF Core 和 EF6 命令的部分)

为了添加迁移,您需要 运行:

>Add-Migration FirstMigration