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
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With