🤔 EF Core in .NET MAUI Mobile Apps: Smart Choice or a Performance Risk? 📱
When building .NET MAUI apps for Android and iOS, the question often arises: should you rely on Entity Framework Core (EF Core) for data management? On one hand, EF Core provides a consistent, developer-friendly approach to managing data models and accessing databases. On the other, mobile apps have unique performance, storage, and efficiency constraints that may raise concerns.
So, is integrating EF Core a good practice in cross-platform mobile apps, or does it introduce complexity and potential performance issues?
Integrating EF Core in .NET MAUI
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "app.db");
optionsBuilder.UseSqlite($"Filename={dbPath}");
}
}
using (var db = new AppDbContext())
{
db.Database.EnsureCreated();
// Add a new item
db.Items.Add(new Item { Name = "Sample Item" });
db.SaveChanges();
// Query items
var items = db.Items.ToList();
}
Injecting the DbContext via Dependency Injection in .NET MAUI
Configure Services in MauiProgram.cs: .NET MAUI applications use a MauiProgram.cs file to configure services, similar to ASP.NET Core. Here’s how you can register the DbContext:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
// Register DbContext with dependency injection
builder.Services.AddDbContext<AppDbContext>(options =>
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "app.db");
options.UseSqlite($"Filename={dbPath}");
});
return builder.Build();
}
}
Injecting AppDbContext in Your Pages or Services: Now that the AppDbContext is registered, you can inject it into your pages or services using constructor injection:
public partial class MainPage : ContentPage
{
private readonly AppDbContext _dbContext;
public MainPage(AppDbContext dbContext)
{
InitializeComponent();
_dbContext = dbContext;
// Example usage
LoadData();
}
private void LoadData()
{
// Fetch data from the database
var items = _dbContext.Items.ToList();
// Handle data as needed
}
}
Recommended by LinkedIn
Benefits of Dependency Injection:
And Migrations?
When working with Entity Framework Core in .NET MAUI applications, you might naturally want to use migrations to manage and evolve your database schema. Migrations allow you to make changes to your data models and apply these changes seamlessly to your underlying database. However, there are some unique challenges with this process in a .NET MAUI context, particularly for Android and iOS platforms:
Lack of Native Support for dotnet ef Commands:
Workarounds and Boilerplate Code:
To address the challenges, developers typically rely on a few workarounds, but these can involve boilerplate and additional complexity:
Boilerplate Code and Maintenance Overhead:
For anyone who's tried using EF Core migrations in .NET MAUI apps, you know it’s not always a smooth journey—especially on Android and iOS. The process often involves complex workarounds, like separate projects for migrations or manual schema updates.
But I’ve been working on a project to simplify and address these challenges head-on. My goal is to make database management in .NET MAUI more intuitive and streamlined, minimizing boilerplate code and reducing complexity.
🌟 If you're interested in exploring these solutions or collaborating, check out the project here:
I’d love to hear your thoughts and experiences. Let’s make EF Core in MAUI a smoother experience together! 💬
Senior Software Developer at University of Cincinnati
3moCarlos Gabriel would you recommend EF Core for a new .Net MAUI project or would you stick with using raw SQL with the SQL Lite library. Aside from the migration issues and additional boilerplate are there any risks in using EF Core in a new medium to large project?
Xamarin/MAUI
6moIts nice but lots of things to do. I prefer to use liteDB.