Step 1. Install EF Core DB Provider
Install NuGet Package for the provider of the DB, we want to access, here we want to access MS SQL Server DB, so we need to install.
- Microsoft.EntityFrameworkCore.SqlServer NuGet package
- Microsoft.EntityFrameworkCore.Design NuGet package
Step 2. Install EF Core Tools
Along with DB provider package, also need to install EF tools to execute EF Core commands.
These make it easier to perform several EF Core-related tasks in your project at design time, such as migrations, scaffolding(demonstrate to solve problem or offering support), etc.
Note: Migrations are a way of defining the schema(representation of a plan or theory) of your database.
In order to execute EF Core commands from Package Manager Console,
search for the Microsoft.EntityFrameworkCore.Tools package (v 5.0.9) from NuGet UI & install.
Step 3. Create Model & Context Classes
We first write the code, which means Model classes, and on the basis of these classes our tables are auto-generated inside the database.
Step 4. Create Connection String
To create these tables in the database we have to define our connection string where we will define our server and database name.
Connection string you need to write inside the appsetting.json file
Step 5. Install dotnet ef tool
Open Developer PowerShell Terminal from visual studio (View → Terminal) and run below command
>dotnet tool install --global dotnet-ef
Step 6. Create Database using Migration
Run the below command in Developer PowerShell Terminal to create the initial migration script.
>dotnet ef migrations add BaseMigration --context EmployeeContext
In the above,"BaseMigration" is descriptive name of the migration. "EmployeeContext" is context name.
We have only created the migration script which is responsible for creating the database and its table. But we’ve not created the actual database and tables. So, let’s execute the migration script and generate the database and tables. Therefore, executing the migration scripts we have to execute ‘update-database’ command.
>dotnet ef database update
Step 7. Update Database using Migration
After initial migration , you add additional fields or change existing field in your model then your model and your database are now out of sync.
We must apply model changes to your database schema. To create a new migration run below command
dotnet ef migrations add AddPaymentkeyToEmployee
In the above, "AddPaymentkeyToEmployee" is descriptive name of the migration. Note that we give migrations a descriptive name, to make it easier to understand the project history later.
Since this isn't the project's first migration, EF Core now compares your updated model against a snapshot of the old model, before the column was added or modified; the model snapshot is one of the files generated by EF Core when you add a migration, and is checked into source control. Based on that comparison, EF Core detects that a column has been added, and adds the appropriate migration.
You can now apply your migration as before:
>dotnet ef database update