![]() ![]() Or you could execute the following dotnet CLI command. For instance, you could execute the following command in the Package Manager Console to erase the most recently applied migration. If you need to undo the last migration, use the Remove-Migration command in the Package Manager Console or the dotnet ef migrations remove command in the terminal. IDGįigure 3: The database and its objects after executing the Database-Update command. This will apply any outstanding database migrations, bringing the schema in sync with your data model.įigure 3 shows the database and the tables created after this command is executed. Update-DatabaseĪlternatively, you can use the following dotnet CLI command. The following code snippet illustrates how you can apply the newest migration to the database. Next, use the Update-Database command at the Package Manager Console to apply the changes to your database. To apply a migration to the database, you must already have created the migration. IDGįigure 2: The auto-generated Migration file. You can examine the resulting code (see Figure 2) to verify that it represents your intended revisions and make any required adjustments. This will build a new migration file in the Migrations folder of your project, including code that represents the modifications to your data model. ![]() Add-Migration MyDemoMigrationĪlternatively, you can execute the following command in the dotnet CLI. For example, to create a new migration called MyDemoMigration in the Package Manager Console, execute the following command. To create a migration, you should use the Add-Migration command in the NuGet Package Manager. Partial void OnModelCreatingPartial(ModelBuilder modelBuilder) Protected override void OnModelCreating(ModelBuilder modelBuilder) Public EFMigrationsDemoDBContext(DbContextOptions Public partial class EFMigrationsDemoDBContext : DbContext cs file named named EFMigrationsDemoDBContext.cs in the EFMigrationsDemo.Data project and enter the following code in there. Create a model class in ASP.NET CoreĬreate a new file named Author.cs in your EFMigrationsDemo.Data project and enter the following code. Next, install the following NuGet package in the starter project, EFMigrationsDemo, to ensure that the migrations will work successfully. Now, install the following three NuGet packages in the EFMigrationsDemo.Data project. IDGįigure 1: The Solution Explorer window at the start of our project. Assuming the initial project was named EFMigrationsDemo, name the class library project EFMigrationsDemo.Data.Īt this time, your Solution Explorer will look like Figure 1 below. We’ll use this class library project to implement EF Core migrations. Install the EF Core NuGet packagesĬreate a class library project in the same ASP.NET Core 7 Web API project we created above. You can work with migrations either from within Visual Studio via the Package Manager Console or by using a command-line tool to run the EF Core CLI commands. Note that migrations are enabled in EF Core by default. Using migrations, developers can precisely log changes to the database schema, implement those changes in a well-organized manner, and undo any changes or modifications if required. ![]() You can take advantage of migrations to enhance or alter your database schema by adding, eliminating, or altering database elements like tables, indexes, columns, and associations. Migrations help you ensure that the database schema and the domain model in your application are in sync. Without migrations, applying these changes to a database would be difficult and error-prone.Ī migration is the process of managing changes to a database schema as they occur over time. These changes could involve adding new tables, modifying existing tables, or deleting tables altogether. In software development, it's common to make changes to the data model as requirements evolve. We’ll use this ASP.NET Core 7 Web API project to work with EF Core migrations in the sections below. Ensure that the check boxes “Enable Open API Support,” “Configure for HTTPS,” and “Enable Docker” are unchecked as we won’t be using these features here.Leave the “Authentication Type” set to “None” (default). In the “Additional Information” window shown next, uncheck the check box that says “Use controllers…” since we’ll be using minimal APIs in this example.Optionally check the “Place solution and project in the same directory” check box, depending on your preferences. ![]() In the “Configure your new project” window, specify the name and location for the new project.In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |