asp.net-core-mvc - 如何在asp.net core 1.0中添加迁移

标签 asp.net-core-mvc 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



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 :
  • 如果您使用 Visual Studio 2017,它们应该已经安装
  • 如果您使用 Visual Studio 2017,则需要使用 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/

    相关文章:

    c# - ASP.NET 5 MVC 6 通用存储库模式

    c# - 使用 text/plain 时不支持的媒体类型

    javascript - Kendo-UI MVC Grid Action Delete 正在路由到 Create 并且 Create 被多次调用

    c# - ASP.NET Core 2.0 禁用自动质询

    MongoDB 驱动程序抛出文件未找到异常

    asp.net-core - Asp.Net core 从 url 获取 RouteData 值

    c# - 与 ASP.NET Core 中的 FromBody 混淆

    c# - 如何在 ASP.NET MVC 6 中继续使用 ModelState 和 RedirectToAction?

    c# - 使用 asp.net 身份在身份服务器 4 中实现角色

    c# - ASP.NET MVC 6 中的属性路由正则表达式约束错误