Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Entity framework core migrations

Suggested Videos
Part 47 - DbContext in entity framework core | Text | Slides
Part 48 - Using sql server with entity framework core | Text | Slides
Part 49 - Repository pattern in asp.net core | Text | Slides

In this video we will discuss the concept of Migrations in Entity Framework Core.


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 
  1. Creating a migration first and then
  2. 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

package manager console in visual studio

Common entity framework core migration commands

We will be using the following 3 common commands to work with migrations in entity framework core.

Command Purpose
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 a Migration in Entity Framework Core

The following command creates the initial migration. InitialCreate is the name of the migration.
Add-Migration InitialCreate

When the above command completes, you will see a file in the "Migrations" folder that contains the name InitialCreate.cs. This file has the code required to create the respective database tables.

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.

entity framework core migrations

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.

asp.net core tutorial for beginners

6 comments:

  1. My newly created database is not showing in my ssms why is that happening...

    ReplyDelete
  2. The PM Console kept giving errors on get-help, I also tried update-help which was not able to update all help files on my machine.
    This answer on stackoverflow helped:
    https://stackoverflow.com/a/60877035

    I had to install EntityFrameworkCore.Tools from the Nuget package manager UI and then the commands started to work.

    ReplyDelete
  3. The DbContext of type 'AppDbContext' cannot be pooled because it does not have a single public constructor accepting a single parameter of type DbContextOptions.

    my class is:
    public class AppDbContext : DbContext
    {
    public AppDbContext(DbContextOptions options) : base (options)
    {

    }

    public DbSet Employees { get; set; }
    }

    ReplyDelete
  4. How can I provide specific schema name for the table in DB?

    ReplyDelete
  5. Since it is for core i recommend use of this link first about installing EntityFrameworkCore.Tools https://stackoverflow.com/questions/47598844/enabling-migrations-in-ef-core

    ReplyDelete
  6. Any One Solve This Error
    The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.14-servicing-32113'. Update the tools for the latest features and bug fixes.
    An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Could not parse the JSON file. Error on line number '8': '"AllowedHosts": "*",
    {'.

    ReplyDelete

It would be great if you can help share these free resources