Using Entity Framework Core 3 migrations

As you have already seen, when developing applications, your models may change frequently when you refactor and finalize your projects. This may lead to a database schema that is out of sync and that needs to be updated manually. You can do this by creating an upgraded script, but this isn't an ideal solution.

Fortunately, Entity Framework Core 3 includes a feature called migrations to help you with this tedious task. It automatically keeps your models and their corresponding database schemas in sync.

After you have updated the models, services, and controllers so that they comply with the preceding constraints and modified the game database context, GameDbContext, accordingly, you are ready to use Entity Framework Core 3 migrations. The following steps will show you how to use Entity Framework Core 3 migrations:

  1. Add a first version of your database schema called InitialDbSchema. To do this, open the NuGet Package Manager by clicking on Tools | NuGet Package Manager | Package Manager Console and execute the Add-Migration InitialDbSchema command:

  1. A new folder called Migrations will be automatically added by Visual Studio. It will contain two autogenerated files that will help you manage and upgrade your database schema in the future:

You can update your database directly from within Visual Studio 2019 if it is accessible from your development environment. The following steps will walk you through the update process:

  1. Go to the Package Manager Console and execute the Update-Database command. This will create a database the first time it is used, or update the database automatically when you change your models:

  1. Then, go to the SQL Server Object Explorer and analyze the database schema that Entity Framework 3 migrations has autogenerated in SQL Server:
  1. After that, right-click on the __EFMigrationsHistory table and select View Data to see how Entity Framework migrations tracks database schema versions:

If your database is not accessible from your development environment (for example, in staging or production), you have to generate a SQL script file.

  1. Go to the Package Manager Console and execute the Script-Migration command to autogenerate a SQL script file, which can be used to create the Tic-Tac-Toe application's database:

  1. After that, execute the generated SQL script file on specific environments like staging and production using your preferred database tools (for example, SQL Server Management Studio, and so on) to create the Tic-Tac-Toe application's database.

You can also use Entity Framework Core 3 migration directly from within your code to ensure that the database is constantly in sync with your models. To do this, you need to call the Migrate method of the GameDbContext instance within the Configure method of the Startup class. Perform the following steps to do so:

  1. Update the Configure method in the Startup class and add the following instructions at the bottom of the method:
        using (var scope =
app.ApplicationServices.GetService<IServiceScopeFactory>()
.CreateScope()) { scope.ServiceProvider.GetRequiredService<GameDbContext>()
.Database.Migrate(); }

This places the game database context's Migrate method as a scoped service that can be resolved at application runtime.

  1. Start the Tic-Tac-Toe application by pressing F5
Note that if a table or property doesn't exist in the database and if the connection string provides enough access rights, Entity Framework Core 3 will automatically create the missing table or the property/column that does not exist.

Now that we've updated the models and the corresponding application database, all the model data will be persisted and the application state is going to be available, even after an application restart. This means that you cannot register already existing emails, you have to add new ones manually, so truncate the database and delete them now.

In the next section, we will focus on creating, reading, updating, and deleting data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.179.220