Suggested Videos
Part 27 - Implement search page in ASP.NET Core | Text | Slides
Part 28 - Install entity framework core in class library project | Text | Slides
Part 29 - Using sql server in razor pages project | Text | Slides
In this video we will discuss creating and executing EF core migrations in a razor pages project.
What is a migration in entity framework core
Migration is an entity framework core feature that keeps the database schema and our application model classes (also called entity classes) in sync.
Creating a migration
If you are using Visual Studio like me, then use Package Manager Console. To launch Package Manager Console in Visual Studio, click on View - Other Windows - Package Manager Console
In the Package Manager Console window, please make sure you have selected the project in which you want the migration to be created. In our case, we created our data access project using a separate class library project (RazorPagesTutorial.Services). So please make sure this project is selected from the Default project dropdownlist.
Use Add-Migration command to create a migration. The following command creates the initial migration. InitialCreate is the name of the migration.
When the above command completes, you will see a file in the "Migrations" folder that ends with the name InitialCreate.cs. This file has the code required to create the respective database tables.
Please note : To have the command auto-completed in the Package Manager Console window, type part of the command and press the TAB key.
Executing a migration
We need to execute the migration code to create the tables. If the database does not exist already, it creates the database and then the database tables. For updating the database, we use Update-Database command. To the Update-Database command we may pass the migration name we want to execute. If no migration is specified, the command by default executes the last migration.
We can confirm that the database and the required tables are created using SQL Server Object Explorer window in Visual Studio.
At the moment our application is still using MockEmployeeRepository which is an in-memory collection of employees. In our upcoming videos we will implement SQLRepository which stores and retrieves employees from sql server employees table that we have just created.
Part 27 - Implement search page in ASP.NET Core | Text | Slides
Part 28 - Install entity framework core in class library project | Text | Slides
Part 29 - Using sql server in razor pages project | Text | Slides
In this video we will discuss creating and executing EF core migrations in a razor pages project.
What is a migration in entity framework core
Migration is an entity framework core feature that keeps the database schema and our application model classes (also called entity classes) in sync.
Creating a migration
If you are using Visual Studio like me, then use Package Manager Console. To launch Package Manager Console in Visual Studio, click on View - Other Windows - Package Manager Console
In the Package Manager Console window, please make sure you have selected the project in which you want the migration to be created. In our case, we created our data access project using a separate class library project (RazorPagesTutorial.Services). So please make sure this project is selected from the Default project dropdownlist.
Use Add-Migration command to create a migration. The following command creates the initial migration. InitialCreate is the name of the migration.
Add-Migration InitialCreate
When the above command completes, you will see a file in the "Migrations" folder that ends with the name InitialCreate.cs. This file has the code required to create the respective database tables.
Please note : To have the command auto-completed in the Package Manager Console window, type part of the command and press the TAB key.
Executing a migration
We need to execute the migration code to create the tables. If the database does not exist already, it creates the database and then the database tables. For updating the database, we use Update-Database command. To the Update-Database command we may pass the migration name we want to execute. If no migration is specified, the command by default executes the last migration.
We can confirm that the database and the required tables are created using SQL Server Object Explorer window in Visual Studio.
At the moment our application is still using MockEmployeeRepository which is an in-memory collection of employees. In our upcoming videos we will implement SQLRepository which stores and retrieves employees from sql server employees table that we have just created.
No comments:
Post a Comment
It would be great if you can help share these free resources