Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Migration in asp.net core 1.0

i have create problem on creation of migration, migration is not created when i created Migration in Package in

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();

    }
}

MadeChanges in 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

Problem occured in Update-Database Command

like image 805
Praful Chauhan Avatar asked Mar 05 '17 07:03

Praful Chauhan


People also ask

How do I enable auto migration in .NET core?

Run the Enable-Migrations –EnableAutomaticMigrations command in Package Manager Console This command has added a Migrations folder to our project. This new folder contains one file: The Configuration class. This class allows you to configure how Migrations behaves for your context.

How do I create a migration file in .NET core?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.

How do I enable-migrations in VS 2019?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.


1 Answers

With EF Core you now have 2 different command line tools, the dotnet cli and the PM console. You can check the official docs for further reference.

When using the dotnet CLI migrations can be added with:

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

So in order to add your new FirstMigration migration you would run:

>dotnet ef migrations add FirstMigration

The same commands are also available for the Package Manager Console:

  • If you use Visual Studio 2017, they should already be installed
  • If you use Visual Studio 2017, you need to install them with Install-Package Microsoft.EntityFrameworkCore.Tools -Pre. (Check the section about installing EF Core and EF6 commands side by side on the documentation)

In order to add a migration you would then run:

>Add-Migration FirstMigration
like image 104
Daniel J.G. Avatar answered Nov 10 '22 01:11

Daniel J.G.