EntityFramework Core Migrations | ASP.NET CORE | CLI Migrations | Package Manager Console Migratio

ASP.NET MVC
1 Apr 202007:58

Summary

TLDRThis tutorial video guides viewers through the process of performing migrations in Entity Framework using .NET. It starts by instructing how to download and install the .NET EF global tool, version 3.1.3, via CLI. The host then demonstrates opening the command prompt, utilizing the 'dotnet ef' command to add and manage migrations, and ensuring the syntax is correct. The video covers creating a migration with a specified name and updating the database with the 'update-database' command. It also addresses how to rectify common errors like spelling mistakes and concludes with verifying the migration's success in SQL Server. Additionally, the video briefly touches on using the Package Manager Console for migrations, providing an alternative to CLI.

Takeaways

  • 😀 The video is a tutorial on how to perform migration in Entity Framework using .NET.
  • 🔧 First step is to download the .NET EF tool globally using the CLI command 'dotnet tool install --global dotnet-ef --version 3.1.3'.
  • 📁 To access the command prompt, navigate to the project folder, type 'CMD' in the address bar, and press enter.
  • 🔎 Use 'dotnet ef --help' to get a list of available commands and options for Entity Framework migrations.
  • 📝 The 'dotnet ef migrations' command is used to manage database context and migrations.
  • 🚀 To add a migration, use the syntax 'dotnet ef migrations add [MigrationName]' where [MigrationName] is the name of the migration.
  • 🛠 After adding a migration, use 'dotnet ef database update' to apply the changes to the database.
  • 🔄 The video demonstrates how to check for unrecognized commands or arguments and correct any spelling mistakes.
  • 📚 It shows how to use the 'dotnet ef database update' command to create a physical table in a local SQL Server database.
  • 🔍 The tutorial also covers how to check the database using SQL Server Management Studio to ensure the migration was successful.
  • 📋 Lastly, the video explains how to use the Package Manager Console for migrations with commands like 'Add-Migration' and 'Update-Database'.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is demonstrating how to perform migration in Entity Framework using .NET Core CLI.

  • How can one download .NET EF globally?

    -To download .NET EF globally, use the command 'dotnet tool install --global dotnet-ef --version 3.1.3' in the CLI.

  • What is the purpose of opening the command prompt from the project folder?

    -Opening the command prompt from the project folder allows you to execute .NET EF commands related to that specific project.

  • How can viewers access the help section for .NET EF commands?

    -Viewers can access the help section for .NET EF commands by typing 'dotnet ef --help' in the command prompt.

  • What command is used to add a new migration?

    -The command used to add a new migration is 'dotnet ef migrations add' followed by the name of the migration.

  • What is the correct syntax for adding a migration in a .NET project?

    -The correct syntax is 'dotnet ef migrations add <MigrationName>' where <MigrationName> is the name you assign to the migration.

  • What command is used to update the database after adding a migration?

    -The command used to update the database is 'dotnet ef database update'.

  • How can one check for a spelling mistake in the commands?

    -One can check for spelling mistakes by referring back to the help section or by carefully reviewing the command syntax.

  • What does the video guide on checking the database after migration?

    -The video guides on checking the database by opening SQL Server Object Explorer and looking for the migration under the specified holder name.

  • What are the two methods demonstrated in the video for using migration in Entity Framework?

    -The two methods demonstrated are using .NET Core CLI and using the Package Manager Console.

  • How can one get help about Entity Framework in the Package Manager Console?

    -One can get help about Entity Framework by typing 'get-help about entity framework' in the Package Manager Console.

Outlines

00:00

🚀 Introduction to Entity Framework Migration with CLI

This paragraph introduces the topic of Entity Framework migration using the Command Line Interface (CLI). The speaker begins by instructing viewers to download the .NET EF tool globally using CLI. They provide a step-by-step guide on how to access the command prompt from the project folder by typing 'CMD' in the address bar and pressing enter. The speaker then explains how to use the 'dotnet ef' command to check for help and the correct syntax for adding migrations. They emphasize the importance of checking the syntax for the 'add-migration' command and provide guidance on using the 'dotnet ef migrations add' command with the correct options and arguments, including the migration name. The paragraph concludes with a brief mention of the 'update-database' command, which is used to manage the database after migrations are added.

05:01

🔍 Database Migration Verification and Using Package Manager Console

The second paragraph covers the process of verifying database migration and introduces an alternative method for performing migrations using the Package Manager Console. The speaker starts by explaining how to check for a newly created physical table in a local SQL Server database after a successful build. They guide the viewer to open SQL Server and verify the presence of the migration in the Object Explorer under the specified database. The paragraph then shifts to discuss how to perform migrations using the Package Manager Console, starting with accessing the help section to understand the available Entity Framework commands. The speaker details the process of adding a migration with a specified name and updating the database using the 'update-database' command. They conclude by highlighting the two primary methods for using migrations in Entity Framework: CLI and Package Manager Console, and thank the viewers for watching.

Mindmap

Keywords

💡ASP.NET

ASP.NET is a framework for building web applications and services. It is used to create dynamic web pages and web applications that interact with databases and other web services. In the context of the video, ASP.NET is likely the platform on which the Entity Framework is being demonstrated, as it is commonly used with ASP.NET applications to manage data.

💡Entity Framework

Entity Framework is an object-relational mapper (ORM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write. The video's main theme revolves around demonstrating the migration feature of Entity Framework, which allows for the management of database schema changes over time.

💡Migration

In the context of Entity Framework, a migration refers to a way to manage changes to the database schema over time for an application. Each migration represents a set of changes to the database, and the video script discusses how to create and apply these migrations using Entity Framework commands.

💡CLI (Command Line Interface)

CLI refers to the command-line interface provided by the .NET Core SDK, which allows developers to perform various tasks through the command line. In the video, the CLI is used to install the Entity Framework tool globally and to execute commands related to migrations, such as 'dotnet ef migrations add'.

💡dotnet tool install

This is a command used in the .NET Core CLI to install a .NET tool. In the script, 'dotnet tool install --global dotnet-ef' is used to install the Entity Framework Core tools globally on the developer's machine, which allows for easy access to the 'dotnet ef' commands from any directory.

💡DbContext

DbContext is a class in Entity Framework that represents a session with the database and allows you to query and save instances of your entities. It is central to Entity Framework as it is the base class for the context that you define in your application. The script mentions 'TB context', likely referring to a specific type of DbContext tailored to the tutorial's database.

💡add migration

The 'add migration' command in Entity Framework is used to scaffold a new migration based on changes detected in the DbContext since the last migration. The script provides a detailed explanation of how to use this command to create a new migration with a specified name.

💡update database

The 'update database' command in Entity Framework applies any pending migrations to the database. This is how changes made in code are reflected in the database schema. The script explains how to use this command after creating a migration to update the database accordingly.

💡Package Manager Console

The Package Manager Console is a tool within Visual Studio that allows developers to run NuGet package query, installation, and removal commands. In the script, it is mentioned as an alternative to the CLI for performing Entity Framework migrations, such as 'add-migration' and 'update-database'.

💡SQL Server

SQL Server is a relational database management system developed by Microsoft. It is mentioned in the script as the type of database server where the migrations are being applied. The video likely demonstrates how to check the created tables and migrations in SQL Server Object Explorer after applying the migrations.

💡get help

The 'get help' command is used in the Package Manager Console to display help information for Entity Framework commands. The script suggests using this command to get information about how to use various Entity Framework commands, such as 'add-migration' and 'update-migration'.

Highlights

Introduction to the process of migration in Entity Framework.

Downloading .NET EF globally using CLI with the command 'dotnet tool install --global dotnet-ef --version 3.1.3'.

Accessing the command prompt by navigating to the project folder and typing 'CMD' in the address bar.

Using 'dotnet ef --help' to understand the available options and commands for Entity Framework migrations.

Explanation of 'dotnet ef options' and 'dotnet ef commands' for managing database context and migrations.

Syntax for adding a migration using 'dotnet ef migrations add <MigrationName>', emphasizing the importance of the migration name.

How to correct syntax errors in the 'add migration' command by referring to the help section.

Building the migration successfully with the correct syntax and initiating the process.

Using 'update database' command to manage the database schema after migrations.

Instructions on how to use the 'update database' command with the correct syntax.

Correcting a spelling mistake in the 'update database' command and ensuring the correct usage.

Verification of the physical table creation in SQL Server LocalDB after updating the database.

Demonstration of checking the database and migrations in SQL Server Object Explorer.

Introduction to using Package Manager Console for Entity Framework migrations.

How to get help in the Package Manager Console with 'get help' and understanding the available commands.

Using 'add-migration' and 'update-database' commands in the Package Manager Console for migrations.

Explanation of the 'up' and 'down' methods in migration classes and their role in model changes.

Applying an example of adding a column to a model and updating the database using migrations.

Conclusion summarizing the two methods of using migrations in Entity Framework: CLI and Package Manager Console.

Transcripts

play00:00

hello everyone welcome on my channel

play00:02

asp.net for today I am going to show you

play00:04

migration in entity framework or so

play00:06

first of all download dotnet EF using

play00:10

CLI dotnet tool installed - - global

play00:13

dotnet EF version 3.1 point 3 after

play00:17

download the dotnet EF it's a global

play00:21

tool to download it easily by using your

play00:24

command line and after that you can open

play00:28

your command prompt and how to open your

play00:31

command from - just go to your project

play00:34

folder there is migration tutorial and

play00:38

after in the search bar or sorry in in

play00:41

the address bar just type CMD and enter

play00:46

you got it that screen after pressing

play00:50

the enter button just taken help dotnet

play00:54

EF - - help and the following command

play00:58

and uses is there with the example you

play01:01

can follow uses that your screen that is

play01:04

not net EF options and command so dotnet

play01:08

EF options is their version health but

play01:11

command is their database TB context and

play01:14

migrations so dotnet EF migrations and

play01:17

the name migrations and add is there but

play01:22

in the migration in the migration ad but

play01:26

migration name is missing in the syntax

play01:28

so just check the syntax of the add

play01:31

migration command so just again you can

play01:35

take help section again so just a type

play01:38

dotnet EF in it migration that is the

play01:46

direct to think you can do in it

play01:48

migration is it right syntax of the

play01:51

migration I think it is strong so just

play01:54

you can take help if you want to take

play01:57

help how to do add migration command in

play02:01

dotnet so you can use dotnet EF

play02:04

migration help so just write it

play02:08

migrations and - - help so related to

play02:13

the my

play02:13

raishin you by normal CLI provide a HAMP

play02:19

section and users also check uses dotnet

play02:22

EF migration option and command option

play02:25

is there and command is there that is

play02:27

the ad so again you can write or net EF

play02:31

migrations and command name that is ad -

play02:37

again have ad how to do ad command in

play02:44

migration that is the right name that is

play02:47

the name of the migration so uses is

play02:49

they are dotnet EF migration ad and the

play02:52

option and argument argument and options

play02:55

so argument is there the name of the

play02:57

migration so just you can write the

play02:59

syntax is dotnet EF migration ad and the

play03:03

name of the migration is the correct

play03:05

syntax to add migration in your visual

play03:09

studio project in dotnet represents so

play03:13

just write your that is the correct

play03:15

syntax dotnet EF migrations ad and the

play03:18

name of the migration that is in it so

play03:22

build started build succeed so migration

play03:25

is completed at my by using the

play03:27

migrations add migrations command and

play03:32

you have to use update database command

play03:35

is also there just you have to check

play03:37

database command to manage the database

play03:40

and users is dotnet EF and command so

play03:44

use dotnet EF and command name is

play03:48

database but how to use update database

play03:51

command so just again check it help

play03:55

dotnet EF - - help database how to use

play04:02

in the.net EF use database there is the

play04:12

following thing is large where you have

play04:16

to check dotnet EF - - help database you

play04:21

can use update the name dotnet t of

play04:24

database and option and comma

play04:26

name is AB drop and database is there so

play04:29

you can apply it directly dotnet EF

play04:32

database and database command that is

play04:35

update so use dotnet EF database and

play04:39

database command name that is database

play04:41

update so this is the syntax to update

play04:44

your database using CLI so unrecognized

play04:50

command or argument data

play04:51

oops the spelling mistake is here dotnet

play04:54

EF database and the name of the database

play04:57

is a is missing in the spelling of

play05:00

database so after update the database

play05:03

you have got a physical table in your

play05:10

database in your sequel's server local

play05:13

DB so build started and will succeed and

play05:19

finally done to and check your database

play05:23

using sequel server so just open

play05:27

database command and your Microsoft your

play05:32

migration example database is there in

play05:34

your sequel server object Explorer and

play05:38

check your init migration under the

play05:41

holder name migration yeah is there and

play05:45

model student app settings database

play05:49

migration example yeah is all things are

play05:52

there and just now how to do migration

play05:56

using package manager console so just

play05:59

you can use gap help section by using

play06:03

get help you got all these command

play06:05

related to entity framework or if you

play06:08

you are using get help without any thing

play06:13

just got a space just like that server

play06:19

help save help update help about entity

play06:24

framework or so just again type it get

play06:27

help

play06:28

about entity framework or so you just

play06:31

get information about entity framework

play06:34

or commands like add migrations remove

play06:38

migrations update migrations

play06:40

Sacre's so add migration and the

play06:46

migration name is first before any

play06:50

changes in the model you got two classes

play06:54

both are empty if nothing changes in the

play06:58

model that is up-and-down methods are

play07:01

empty in the first migration class

play07:04

because here is nothing changes but if

play07:08

you change anything in the model then up

play07:11

section up method and the our overridden

play07:14

up method and overhead and down method

play07:16

is taking some code and statements you

play07:20

can say just after that again at

play07:24

migrations command applied you got a

play07:27

simple syntax add column string email

play07:30

now after that you got and you have to

play07:34

apply update database command again by

play07:37

using package manager console so just

play07:40

done it so these this is that two way to

play07:44

use migration in entity framework or the

play07:50

first is CLI and the second one is

play07:53

package manager console so thank you

play07:55

guys for watching this video keep

play07:57

watching

Rate This

5.0 / 5 (0 votes)

Related Tags
Entity FrameworkDatabase MigrationsCLI CommandsPackage ManagerMigration TutorialDotnet EFData ManagementSoftware DevelopmentMigration ToolsTutorial Video