Telerik blogs

In Part 2, we’ll cover how to populate Telerik UI for .NET MAUI with data and EF CORE migrations.

In the previous post, we demonstrated how we can achieve CRUD operations using Telerik DataGrid and EF Core. However, in the real world, the requirements and code are changing, sometimes unplanned, and we need to update the database accordingly. This may not be an easy task unless we use EF Core migrations.

And that’s is what this post is about. We will review how to use EF Core migrations in a .NET MAUI application with Telerik UI included.

What are EF Core Migrations and Do We Need Them?

EF Core migrations are a feature of EF Core that enables developers to change the database schema over time as the application changes. Or simply: the code that instructs EF Core of database structure to create and use.

With this, when we change the models we just have to auto-generate additional migrations for EF Core to modify the existing database accordingly.

  • With migrations, you can easily apply or revert database changes as your application evolves, without losing existing data.
  • EF Core migrations automatically generate and execute the necessary SQL scripts to update the database schema, so you don’t have to write manual scripts or worry about data loss.

So, it is up to us to decide whether to use them. If you do not need them, then you can stop right here. But if you do need them, then we need to do more.

Create EF Core Migrations

To create EF Core migrations, we’d also need to install the package Microsoft.EntityFrameworkCore.Tools.

The thing is, we cannot install these NuGets into our single .NET MAUI project as it has some limitations. This would lead to the following exception when we try to execute the real migrations:

Startup project EFCoreTelerik targets platform Android. The Entity Framework Core Package Manager Console Tools don't wupport this platform.

But, don’t worry—this is what the following part is: how to “work around” it.

Reconstruct the Project

The solution is simple—use a basic console app as a target to execute the migration commands.

However, as we want both a .NET MAUI app and the console app to have access to the DataContext and models, we will also create a shared class library and move them there.

So our project will have now the following structure:

  • EFCoreTelerik – the original .NET MAUI app representing the client
  • EFCoreTelerik.Console – the dummy client used to execute the migrations (migrator)
  • Shared – a shared library to save the data context

Let’s Prepare the 2 New Projects

Shared Project

  1. Add the needed packages:
    SQLitePCLRaw.bundle_e_sqlite3
    Microsoft.EntityFrameworkCore.Sqlite

  2. Move DataContext and model class ToDoItem.cs there.

File structure under Shared has dependencies, migrations, models, DbDataContext.cs. Under Models is TodoItem.cs

Note: The Migrations folder will be automatically created after we execute the migrations.

EFTelerik.Console

Create an empty console app with three NuGets:

  • SQLitePCLRaw.bundle_e_sqlite3
  • Microsoft.EntityFrameworkCore.Sqlite
  • Microsoft.EntityFrameworkCore.Sqlite.Tools

Now, the Actual Migration

Here the steps vary per the tooling:

VS for Windows

  • Change your startup project to the console project.
  • Open Package Manager Console: Go to View -> Other Windows -> Package Manager Console.
  • Set the default project to Shared. EF will look for the context model inside and add migrations there.
  • Enter the following command to create the initial migration:

VS for Mac

  • Right-click your solution, then select “Open In Terminal.”
  • Execute the command:
dotnet ef migrations add Initial -s EFCoreTelerikClient -p Shared  -c EFCoreTelerik.Shared.DbDataContext

More to Read

These were the main points to consider of when start configuring the data related operations to a .NET MAUI app. To dive deeper into the topics that we covered you can refer to the articles below:

The article uses Telerik .NET MAUI DataGrid, but the steps above are valid for loading with data any of the 60+ Telerik UI for .NET MAUI components. More examples with Telerik UI can be found at Telerik Controls Samples, developer-focused examples and Telerik documentation.

If you are curious what else come to the package download and try it now. 👇

Try Telerik UI for .NET MAUI


Rossitza-Fakalieva
About the Author

Rossitza Fakalieva

Rossitza Fakalieva was a leader of Telerik engineering teams working with XAML technologies—Telerik UI for WPF, Telerik UI for Xamarin and Telerik UI for .NET MAUI.


 

Related Posts

Comments

Comments are disabled in preview mode.