25 Sept 2019

How to add Entity Framework Core into ASP.Net Core project

Installation

1. Open a terminal and navigate into the project's folder
2. Use the following .NET Core CLI command from the operating system's command line to install or update the EF Core SQL Server provider

Global EF installation

dotnet ef must be installed as a global or local tool. Most developers will install dotnet ef as a global tool with the following command:

dotnet tool install --global dotnet-ef

https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli

=============

Project

for MS SQL
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

or for SQLite
dotnet add package Microsoft.EntityFrameworkCore.Sqlite 

modifier
-v // version
-v 2.2.0

3. Install tools to carry out EF Core-related tasks in your project, like creating and applying database migrations, or creating an EF Core model based on an existing database

dotnet add package Microsoft.EntityFrameworkCore.Design

For ASP.NET Core apps, this package is included automatically. Database provider design-time packages such as Microsoft.EntityFrameworkCore.SqlServer.Design are no longer required or supported from EF Core 2.0 and later, but aren't automatically removed when upgrading the other packages

4. To get the Package Manager Console tools for EF Core, install

dotnet add package Microsoft.EntityFrameworkCore.Tools


or install packages via NuGet

=============

Add Migrations

After you've defined your initial model, it's time to create the database. To add an initial migration, run the following command:

dotnet ef migrations add InitialCreate 

Update the database
Next, apply the migration to the database to create the schema.

dotnet ef database update

Remove a migration
Sometimes you add a migration and realize you need to make additional changes to your EF Core model before applying it. To remove the last migration, use this command. After removing the migration, you can make the additional model changes and add it again.

dotnet ef migrations remove

Revert a migration
If you already applied a migration (or several migrations) to the database but need to revert it, you can use the same command to apply migrations, but specify the name of the migration you want to roll back to.

dotnet ef database update LastGoodMigration

=============

Commands

dotnet ef migrations add
dotnet ef migrations list
dotnet ef migrations script
dotnet ef dbcontext info
dotnet ef dbcontext scaffold
dotnet ef database drop
dotnet ef database update


No comments:

Post a Comment

Popular Posts