How to work with EF Core migrations in ASP.NET Core

Uncategorized

An object-database mapper for.NET Core, Entity Framework Core is the open-source, cross-platform counterpart of the Entity Framework ORM (object-relational mapper) for.NET. Among the significant functions of EF Core is the capability to carry out migrations to upgrade your database schemas and guarantee they are kept in sync with the data designs of your applications.In this short article, we will explore the rudiments of using EF Core migrations in ASP.NET Core 7 applications. To utilize the code examples offered in this post, you must have Visual Studio 2022 set up in your system. If you don’t already have a copy, you can download Visual Studio 2022 here. Produce an ASP.NET Core Web API task in VisualStudio 2022 First of all, let’s produce an ASP.NET Core 7 project in Visual Studio 2022. Follow these steps: Introduce the Visual Studio 2022 IDE. Click “Produce brand-new project. “In the”Create new task

  • “window, select”ASP.NET Core Web API”
  • from the list of design templates showed. Click Next. In the”Configure your new project “window, specify the name and place for the new project.
  • Optionally check the “Place option and project in the same directory”check box, depending on your choices. Click Next. In the “Extra Details”window revealed next, uncheck the check box that states”Use controllers …”since
  • we’ll be using minimal APIs in this example. Leave the” Authentication Type “set to” None “(default ). Guarantee that the check boxes” Enable Open API Support,””Set up for HTTPS,”and “Enable Docker” are untreated as we won’t be utilizing
  • these features here. Click Create. We’ll utilize this ASP.NET Core 7 Web API job to deal with EF Core migrations in the areas below.What are EF Core migrations?In software application development, it’s common to make changes to the information model as requirements develop. These modifications could include including brand-new tables, customizing existing tables, or deleting tables completely. Without migrations,

    applying these modifications to a database would be challenging and error-prone. A migration is the process of handling modifications to a database schema as they occur gradually. Migrations assist you make sure that the database schema and the domain design in your application remain in sync. You can benefit from

    migrations to enhance or change your database schema by adding, eliminating, or modifying database components like tables, indexes, columns, and associations. Using migrations, designers can precisely log modifications to the database schema, implement those changes in a well-organized way, and reverse any changes or modifications if required.Note that migrations are allowed in EF Core by default. You can work with migrations either from within Visual Studio through the Package Supervisor Console or by utilizing a command-line tool to run the EF Core CLI commands. Set Up the EF Core NuGet packages Create a class library task in the same ASP.NET Core 7 Web API project we created above. We’ll utilize this class library task to carry out EF Core migrations. Presuming the initial task was named EFMigrationsDemo, name the class library job EFMigrationsDemo.Data.At this time, your Solution Explorer will appear like Figure 1 below.

    IDG Figure 1: The Solution Explorer window at the start of our job. Now, install the following 3 NuGet bundles in the EFMigrationsDemo.Data task. Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Next, install the following NuGet plan in the starter job, EFMigrationsDemo, to guarantee that the migrations will work effectively. Microsoft.EntityFrameworkCore.Design Create a design class in ASP.NET Core Create a brand-new file called Author.cs in your EFMigrationsDemo.Data job and go into the following code.using System.ComponentModel.DataAnnotations; namespace EFMigrationsDemo.Data public partial class Author Produce the data context in ASP.NET Core Produce a new.cs submit called EFMigrationsDemoDBContext.cs

    in the EFMigrationsDemo.Data job and

    get in the following code in there.using Microsoft.EntityFrameworkCore; namespace EFMigrationsDemo.Data public partial class EFMigrationsDemoDBContext: DbContext Create a migration using EF Core To produce a migration, you must utilize the Add-Migration command in the NuGet Plan Supervisor. For example, to create a new migration called MyDemoMigration in the Bundle Manager Console, execute the following command.Add-Migration MyDemoMigration Additionally, you can carry out the following command in the dotnet CLI. dotnet ef migrations add MyDemoMigration This will construct a new migration file in the Migrations folder of your job, including code that represents the modifications to your data design. You can take a look at the resulting code(see Figure 2)to validate that it represents your designated revisions and make any required adjustments.

    IDG Figure 2: The auto-generated Migration file. Use the migration utilizing EF Core To use a migration to the database, you must already have created the migration. Next, use the Update-Database command at the Package Manager Console to apply the modifications to your database. The following code snippet highlights how you can use the newest migration to the database.Update-Database Additionally, you can use the following

    dotnet CLI command.dotnet ef database upgrade This will use any outstanding database migrations, bringing the schema in sync with your data model.Figure 3 shows the database and the tables created after this command is carried out. IDG Figure 3: The database and its objects after carrying outef core migrations 02 the

    Database-Update command. Eliminate a migration utilizing

    EF Core If you require to reverse the last migration

    , use the Remove-Migration command in the Package Manager Console or the dotnet ef migrations eliminate command in the terminal. For example, you could perform the following command in the Plan Manager Console to erase the most just recently applied migration.Remove-Migration Or you could execute the following dotnet CLI command.dotnet ef migrations get rid of The Remove-Migration command is adept at removing the last-applied migration and updates the database schema

    appropriately to match the previous migration. In other words, the command will erase the most current migration

    from the Migrations folder of the job and alter the database schema to reflect theef core migrations 03 migration prior to it.Revert a migration using EF Core You may often need to revert modifications made in your database to an earlier migration. To update the database to a previous state, you can utilize the following syntax.update-database For this reason, to go back modifications made to the database to the migration called MyInitialMigration, you would utilize the following command.Update-database MyInitialMigration Alternatively, you might use the following dotnet CLI command.dotnet ef database update MyInitialMigration Migrations are a core

    component of EF Core that enables designers to manage database

    schema modifications in an organized

    and efficient manner. By using migrations, you can use modifications to your database schema when required, revert such modifications when needed, and display changes to the database schema with time. Copyright © 2023 IDG Communications, Inc. Source

  • Leave a Reply

    Your email address will not be published. Required fields are marked *