filmov
tv
Entity framework core migrations
Показать описание
In this video we will discuss the concept of Migrations in Entity Framework Core.
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
Text version of the video
Slides
ASP.NET Core Text Articles & Slides
ASP.NET Core Tutorial
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
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 class) in sync.
If you have not executed at-least the initial migration in your application you might get the following SqlException
SqlException: Cannot open database "EmployeeDB" requested by the login.
This is because we do not have the database created yet. One way to create the database is by
Creating a migration first and then
Executing that migration
To work with migrations, we can either use the Package Manager Console (PMC) or the .NET core command-line interface (CLI). If you are using Visual Studio like me, then use the Package Manager Console.
To launch Package Manager Console in Visual Studio, click on View - Other Windows - Package Manager Console
Common entity framework core migration commands
We will be using the following 3 common commands to work with migrations in entity framework core.
get-help about_entityframeworkcore - Provides entity framework core help
Add-Migration - Adds a new migration
Update-Database - Updates the database to a specified migration
Please note : You can use get-help command with any of the above commands. For example get-help Add-Migration provides help for Add-Migration command.
Creating Migration in Entity Framework Core
The following command creates the initial migration. InitialCreate is the name of the migration.
Add-Migration InitialCreate
Please note : To have the command auto-completed in the Package Manager Console window, type part of the command and press the TAB key.
Update-Database in Entity Framework Core
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.
After the migration is executed, when you navigate to the application we no longer get the following SqlException
SqlException: Cannot open database "EmployeeDB" requested by the login.
This is because the EmployeeDB is created when the migration is executed. We can confirm this in SQL Server Object Explorer window in Visual Studio.
At the moment we do not have any data in the Employees table. In our next video, we will discuss how to seed the database table with initial data.
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
Text version of the video
Slides
ASP.NET Core Text Articles & Slides
ASP.NET Core Tutorial
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
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 class) in sync.
If you have not executed at-least the initial migration in your application you might get the following SqlException
SqlException: Cannot open database "EmployeeDB" requested by the login.
This is because we do not have the database created yet. One way to create the database is by
Creating a migration first and then
Executing that migration
To work with migrations, we can either use the Package Manager Console (PMC) or the .NET core command-line interface (CLI). If you are using Visual Studio like me, then use the Package Manager Console.
To launch Package Manager Console in Visual Studio, click on View - Other Windows - Package Manager Console
Common entity framework core migration commands
We will be using the following 3 common commands to work with migrations in entity framework core.
get-help about_entityframeworkcore - Provides entity framework core help
Add-Migration - Adds a new migration
Update-Database - Updates the database to a specified migration
Please note : You can use get-help command with any of the above commands. For example get-help Add-Migration provides help for Add-Migration command.
Creating Migration in Entity Framework Core
The following command creates the initial migration. InitialCreate is the name of the migration.
Add-Migration InitialCreate
Please note : To have the command auto-completed in the Package Manager Console window, type part of the command and press the TAB key.
Update-Database in Entity Framework Core
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.
After the migration is executed, when you navigate to the application we no longer get the following SqlException
SqlException: Cannot open database "EmployeeDB" requested by the login.
This is because the EmployeeDB is created when the migration is executed. We can confirm this in SQL Server Object Explorer window in Visual Studio.
At the moment we do not have any data in the Employees table. In our next video, we will discuss how to seed the database table with initial data.
Комментарии