Migrations In Entity Framework Core
Migrations In Entity Framework Core
6 April 2022
A Brief Intro About Entity Framework Core (EF Core)
Entity Framework Core is an ORM (object relational mapper) which can be used to access data from various data sources. Entity Framework was the initial framework which was part of classic .NET Framework (predecessor of .NET Core). Entity Framework Core is the open source, cross platform version of Entity Framework.
Entity Framework Core Migrations
Entity Framework Core provides a way to communicate with data stores using C# objects.
Earlier, database creation, tables creation, seed data population were done using SQL scripts. The need of scripts are eliminated by Entity Framework Core..
We can create a custom DbContext implementation for creating a database, that will specify list of DbSets (a DbSet basically maps to table from relational database). DbSet is generic type, it accepts POCO class as type parameter. This POCO class specify public properties and these properties are columns of the SQL table. These set of classes are commonly known as EF Core Model.
We can generate migrations using Entity Framework Core Tool. A migration is a script that is generated to keep the database in sync with the DbContext code. A migration is used to incrementally apply schema changes to database so as to keep database compatible with EF Core model.
When you generate the migration script, a new C# file is generated. It’s name has date and timestamp indicating when this file was generated. The file contains C# code to apply schema changes.
There are two ways to generate migrations
- Using dotnet CLI tools (this is recommended approach for .NET Core apps)
- By Using NuGet package manager console which is used for EF6 – .NET Framework like experience or for working from Visual Studio Package Manager.
In this blog, we are going to look at dotNET CLI tools and commands to generate and apply migrations.
The dotnet CLI provides entity framework tool which is used for creating migration. In the below code snippet, the first command is used to install the tool and second command is used to update the already installed tool.
|## For installing the dotnet CLI ef tool|
|dotnet tool install –global dotnet-ef|
|## For updating already existing tool|
|dotnet tool update –global dotnet-ef|
|## The project which contains EF Core Models|
|## Should have reference to this package|
|## For using EF Migration commands using above tool|
|dotnet add package Microsoft.EntityFrameworkCore.Design|
This tool can be used for following purposes mentioned below:
- For creating new migration– this is used to generate new migration file
- For removing an existing migration – this is used to remove (delete) the migration file.
- For applying migrations to a database – this is used for creating new or altering existing database
- For removing existing database – this is used for dropping existing database
The below code snippet shows all these commands. These commands can be very helpful when we are working on creating a new database using entity framework.
|## For the commands Given Below,|
|## TestDbContext – This is a class which is derived from DbContext|
|## For deleting an existing database|
|dotnet ef database update 0 –context TestDbContext|
|## For removing an existing migrations|
|dotnet ef migrations remove –context TestDbContext|
|## For creating a new EF migration, with name InitialCreate|
|dotnet ef migrations add InitialCreate –context TestDbContext|
|## For applying migrations to the database|
|## All tables specified in SampleDbContext will be created in the database|
|dotnet ef database update –context TestDbContext|