Entity framework core migrations

preview_player
Показать описание
In this video we will discuss the concept of Migrations in Entity Framework Core.

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.

Text version of the video

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

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

Common entity framework core migration commands

We will be using the following 3 common commands to work with migrations in entity framework core.
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 Migration in Entity Framework Core

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

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.

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.
Рекомендации по теме
Комментарии
Автор

I am always amazed by the amount of time and energy you invest while preparing these materials.Still for free.Thank you so much.

naodagere
Автор

I cant use Add-Migration before installed
in PM console: Install-Package

fnfgung
Автор

Couldn't figure out for the life of me how to actually create the database. Didn't know about update-database. Thank you so much for this video!

stephenriding
Автор

simple, and straight to the point! Kudos!

hzubaca
Автор

My teacher thank you very much
Very useful lectures too
Thank you from the depths

honeesh
Автор

You are making great .NET Engineers.
Be blessed.

yasirirfan
Автор

Is it possible to use the PM to create and manage stored procedures? I have been told, that using stored procedures, is the safest way, to interact with the SQL database.

Thank you for some very great toturials!!

Автор

Question about field creation, the filed for department is enum and is nullable, in the video it creates the filed in the table as integer and nullable if false. I think I understand the integer part but should it be nullable because the field is nullable? What if it was desired to have the field nullable how would it be accomplished to signify nullable field during migration? Thank you for all your hard work.

eagleusa
Автор

Çok teşekkür ediyorum. Çok iyi anlatıyorsunuz.

sinanukuslu
Автор

very informative nice video thanks a lot

MmMm-tgmq
Автор

"Format of the initialization string does not conform to specification starting at index 0." I get an error

MuhammedAli-nutb
Автор

How can I update a dbcontext if I´m using database first? I hava my database ready, just need to update de context on EFCore 3.1, any help please?

fermidu
Автор

Really good material.
But I have a question.
When I have only schema updates, everything works okay.
Now I have seed methods to add some records to the database.(For example menu items, rights, roles)
And I tried to run add-migration whenever I have updates in schemas.
It worked yet.
But I wanted to add some more records. I mean I updated the seed methods.
And then run
add-migration update_seed.
After that, update-database failed.
Would you help me with how to handle the cases?

xinjinli
Автор

I have an error...
The specified deps.json does not exist

lin
Автор

thank you, Sir, it's clear now! :)

halivudestevez
Автор

Thank you, so much, but in .net core 3 and newer version we should install EntityFrameworkCore.Tools and then use Add-Migration

hamedbakhtiari
Автор

Getting this exception while using "Add-Migration" command Method 'Create' in type from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=3.1.3.0, Culture=neutral, does not have an implementation. anyone can help?

prithupaul
Автор

If you are using MacOS version of Visual Studio, you might stuck at this episode. You won't find SS Object Explorer nor Package Manager Console because none of those are supported. Here is what I did:
1. Install NuGet PowerShell Core Console:

2. Install Entity Framework Core tools:
3. Install SQL Server on Mac (Docker + Azure Data Studio):
4. Change the connection string to something like this:


kissL
Автор

How can I create a table within an existing schema when I run update-Database. Please help

khoinguyen
Автор

thanks alot My teacher need tutorial for unit test in asp.net core

AhmedGamalGIS