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.
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.
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.
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:
But, don’t worry—this is what the following part is: how to “work around” it.
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 clientEFCoreTelerik.Console
– the dummy client used to execute the migrations (migrator)Shared
– a shared library to save the data contextAdd the needed packages:
• SQLitePCLRaw.bundle_e_sqlite3
• Microsoft.EntityFrameworkCore.Sqlite
Move DataContext and model class ToDoItem.cs there.
Note: The Migrations folder will be automatically created after we execute the migrations.
Create an empty console app with three NuGets:
SQLitePCLRaw.bundle_e_sqlite3
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Sqlite.Tools
Here the steps vary per the tooling:
VS for Windows
VS for Mac
dotnet ef migrations add Initial -s EFCoreTelerikClient -p Shared -c EFCoreTelerik.Shared.DbDataContext
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 the 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. 👇
Rossitza Fakalieva is a Technical Manager, Microsoft MVP in Developer Technologies and a Director of the Bulgarian chapter of the global Women Who Code organization. She previously worked on the Telerik engineering team and defines herself as .NET enthusiast. She loves to empower others to grow in their career and in the tech field—by teaching, by delivering courses and presentations, and as part of her daily job.