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.
Data Access and Data Store
Every application generates and/or processes some data. The generated data or the processed data mostly are somehow persisted so that it can be used later by the same application or some other application.
For example, a web application might allow users to register for creating new account in the site. Then it might allow to add or update their personal information, etc.
Application uses data access code to interact with data stores. A data store is any place where this data is stored. Some examples can be flat file (technically possible), Mongo DB, CosmosDB, a Sqllite database, MySQL database, SQL Server database, etc.
EF Core supports many database engines/data stores. Check documentation – Database Providers – for more details. Application connects to data store using connection string.
Entity Framework Core Migrations
Entity Framework Core provides a way to interact with data stores using C# objects.
Traditionally, database creation, tables creation, seed data population is done using SQL scripts. Entity Framework core eliminates the need of SQL script for these tasks.
For defining a database, we can create a custom DbContext implementation, which will specify list of DbSets (a DbSet basically maps to table from relational database). DbSet is a generic type, it accepts a POCO class as type parameter. This POCO class can specify public properties and these properties would be columns of the SQL table. These set of classes is commonly referred to as EF Core Model.
Then Entity Framework Core tool can be used to generate migrations. A migration is basically a script that is generated to keep the database in sync with the DbContext code. A migration can be used to incrementally apply schema changes to database so as to keep database compatible with EF Core model.
Physically, when you generate migration script, a new C# file is generated. It’s name has date and timestamp signifying when this file was generated. This file has C# code to apply schema changes.
There are two ways to generate migrations
- Using dotnet CLI tools (the recommended approach for .NET Core apps)
- Using NuGet package manager console (for EF6 – .NET Framework like experience or for working from Visual Studio Package Manager)
In this article, we are going to have look at
dotnet CLI tool and common commands to generate and apply migrations.
Install EF Core Tool
dotnet CLI provides entity framework tool which can be used for generating migration. From below snippet, first command shows how to install the tool and second command is to update the already installed tool.
This tool can be used for mainly purposes mentioned below:
- To create a new migration– this is to generate new migration file
- To remove an existing migration – this is to remove (deletes) the migration file.
- To apply migrations to a database – this is equivalent of creating new or altering existing database
- To remove existing database – this is equivalent of dropping existing database
Below code snippet shows all these commands. These commands can be very handy when we are working on creating a new database using entity framework.
If you want you can also refer couple of previous posts on this topic to know
- EF Core Migrations with DbContext in Separate Library
- .NET Core 3 and Entity Framework Core Migrations
I hope you find this information useful. Let me know your thoughts.