Introduction to Core Migrations in Asp.Net Core


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

Migration is a way to keep your database schema in sync with the entity framework core model by maintaining data.

Table of Content

Entity framework core migration is a set of commands and it is executed in .NET CLI (Command Line Interface) and also in NuGet Package Manager.

There are some problems with some strategies like just assume you have already data or you need to seed data or any existing stored procedure and triggers in your database. These strategies are used to drop the full database and need to recreate the full database so you lose your old data and some other database objects to prevent this problem. EF core is introduced a migration tool that automatically updates the database schema whenever your model changes. It is done by updating the database schema without losing any current data or any other object.

Entity framework core migration has two types.

Automated Migration

EF core is introduced us to automated migration, using it we don’t have to do database migration manually for each and every change which you make in your domain classes.

It is built by just executing the one command “enable-migration” in the CLR or also in the Package manager console.

Code-based Migration

The Code-base migration is provided more and deep control on the migration and it also allows you to configure additional things like setting a by the default value of any column and configure a computed column.

To use code-based migration first, you need to execute some code in order in the Package Manager Console in Visual Studio:

  1. nable-Migrations : It enables the migration in your project by creating a Configuration class.
  2. Add-Migration : It creates a new migration class as per the default name with the Up () and Down () methods.
  3. Update-Database : Update-Database is performing the last migration file created by the Add-Migration command and implements changes to the database schema.

To use the Code Based migration you firstly need to execute the command “enable-migration” in the console.

Just assume you have created a normal Entity framework core application that has the following simple model:

public class Employee
{
    public int EmployeeId{ get; set; }
    public string EmployeeName{ get; set; }
}


During your application development period, you have the option to drop and create API and changing your models but now the application is in the production phase. In this phase you can take care of schema and data you work without losing the data and schema or any other database object.

For using migration, you need to install some packages and tools first:

Generally, most of the recommendations is of using .NET core CLI tools with its platforms independent, so it is working on all platforms. But you will be more comfortable with Entity Frame Work migration and you can also do it using the Package Manager Console tool.

Creating Migration

First, as we see above we defined the initial domain classes at this point we don’t have any database which stores our data from our domain classes. So first, we need to create a migration. Some steps are below for adding migration:

Initially, you need to open the package manager console from Tools menu in this menu NuGet Package Manager in this open Package Manager Console and execute the below command:

PM>add-migration FirstMigration

In the above commands, FirstMigration is the name of a migration. This will create three files in the migration folder of your project, which is shown below:

  1. time-stamp_Migration Name.cs : Main migration file which contains migration operation in the UP () and Down () methods. UP () method contains creating object and Down () method includes removing objects.
  2. time-stamp_Migration Name.Designer.cs : his is migration meta data file which contains the information used by EF core.
  3. contextclassname ModelSnapshot.cs : This file contains a snapshot of your current model.

Create the database and update the database using migration

Entity framework is creating some file to your project when migration is called and it is good to know what specifically entity framework core is generated but we will skip it for now.

At this step, you need to create a database and schema using the migration and it is done like below:

Just writing the below command in your CLI or Package Manager Console tool.

PM>Update-Database

Op zoek naar een vertrouwd ASP.Net-softwareontwikkelingsbedrijf ?

Uw zoekopdracht eindigt hier.


If you run in CLI the need to write the below command:

>dotnet ef database update

This Update command is use to create database according to your context and domain classes and the migration snapshot which is created by the add-migration command.

Now the application is ready to run on the new database so you didn’t have to write any single line of SQL commands. This is for local development but it is low suitable for the production phase.

If Migration runs for the first-time then it also creates one table its name is __EFMigrationHistory. This table stores the name of your all migrations and their time when it is applied to the database.

Removing a Migration

You can also remove the last run migration if it is not applied to your database. You can use the below command to remove the last created migration and also for revert model snapshot.

If you use the Package Manager Console then use the below command:

PM>remove-migration

If you are done using CLI then use the below command:

>dotnet ef migrations remove

These commands will remove the last migration and revert the model snapshot to the previous migration. If the last migration is already applied to the database, then it throws the following exception.

These commands remove the last migration and also revert the model snapshot to the last migration.

NOTE : If your migration is already applied to your database then it is throwing the below error.

The migration has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration

Conclusion

After going through this blog, you can understand what is migration and how to use migration in entity framework core. The migration is also used with the entity framework using the migration you can create a database and also maintain a database without writing a single line of SQL code and manage database objects without losing the data. Also, we have shown how to remove migration and update migration in the entity framework in this blog.

Introduction to Core Migrations in Asp.Net Core

Migration is a way to keep your database schema in sync with the entity framework core model by maintaining data.

Table of Content

Entity framework core migration is a set of commands and it is executed in .NET CLI (Command Line Interface) and also in NuGet Package Manager.

There are some problems with some strategies like just assume you have already data or you need to seed data or any existing stored procedure and triggers in your database. These strategies are used to drop the full database and need to recreate the full database so you lose your old data and some other database objects to prevent this problem. EF core is introduced a migration tool that automatically updates the database schema whenever your model changes. It is done by updating the database schema without losing any current data or any other object.

Entity framework core migration has two types.

Automated Migration

EF core is introduced us to automated migration, using it we don’t have to do database migration manually for each and every change which you make in your domain classes.

It is built by just executing the one command “enable-migration” in the CLR or also in the Package manager console.

Code-based Migration

The Code-base migration is provided more and deep control on the migration and it also allows you to configure additional things like setting a by the default value of any column and configure a computed column.

To use code-based migration first, you need to execute some code in order in the Package Manager Console in Visual Studio:

  1. nable-Migrations : It enables the migration in your project by creating a Configuration class.
  2. Add-Migration : It creates a new migration class as per the default name with the Up () and Down () methods.
  3. Update-Database : Update-Database is performing the last migration file created by the Add-Migration command and implements changes to the database schema.

To use the Code Based migration you firstly need to execute the command “enable-migration” in the console.

Just assume you have created a normal Entity framework core application that has the following simple model:

public class Employee
{
    public int EmployeeId{ get; set; }
    public string EmployeeName{ get; set; }
}


During your application development period, you have the option to drop and create API and changing your models but now the application is in the production phase. In this phase you can take care of schema and data you work without losing the data and schema or any other database object.

For using migration, you need to install some packages and tools first:

Generally, most of the recommendations is of using .NET core CLI tools with its platforms independent, so it is working on all platforms. But you will be more comfortable with Entity Frame Work migration and you can also do it using the Package Manager Console tool.

Creating Migration

First, as we see above we defined the initial domain classes at this point we don’t have any database which stores our data from our domain classes. So first, we need to create a migration. Some steps are below for adding migration:

Initially, you need to open the package manager console from Tools menu in this menu NuGet Package Manager in this open Package Manager Console and execute the below command:

PM>add-migration FirstMigration

In the above commands, FirstMigration is the name of a migration. This will create three files in the migration folder of your project, which is shown below:

  1. time-stamp_Migration Name.cs : Main migration file which contains migration operation in the UP () and Down () methods. UP () method contains creating object and Down () method includes removing objects.
  2. time-stamp_Migration Name.Designer.cs : his is migration meta data file which contains the information used by EF core.
  3. contextclassname ModelSnapshot.cs : This file contains a snapshot of your current model.

Create the database and update the database using migration

Entity framework is creating some file to your project when migration is called and it is good to know what specifically entity framework core is generated but we will skip it for now.

At this step, you need to create a database and schema using the migration and it is done like below:

Just writing the below command in your CLI or Package Manager Console tool.

PM>Update-Database

Op zoek naar een vertrouwd ASP.Net-softwareontwikkelingsbedrijf ?

Uw zoekopdracht eindigt hier.


If you run in CLI the need to write the below command:

>dotnet ef database update

This Update command is use to create database according to your context and domain classes and the migration snapshot which is created by the add-migration command.

Now the application is ready to run on the new database so you didn’t have to write any single line of SQL commands. This is for local development but it is low suitable for the production phase.

If Migration runs for the first-time then it also creates one table its name is __EFMigrationHistory. This table stores the name of your all migrations and their time when it is applied to the database.

Removing a Migration

You can also remove the last run migration if it is not applied to your database. You can use the below command to remove the last created migration and also for revert model snapshot.

If you use the Package Manager Console then use the below command:

PM>remove-migration

If you are done using CLI then use the below command:

>dotnet ef migrations remove

These commands will remove the last migration and revert the model snapshot to the previous migration. If the last migration is already applied to the database, then it throws the following exception.

These commands remove the last migration and also revert the model snapshot to the last migration.

NOTE : If your migration is already applied to your database then it is throwing the below error.

The migration has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration

Conclusion

After going through this blog, you can understand what is migration and how to use migration in entity framework core. The migration is also used with the entity framework using the migration you can create a database and also maintain a database without writing a single line of SQL code and manage database objects without losing the data. Also, we have shown how to remove migration and update migration in the entity framework in this blog.

  • Pin It
TOP