How to do the migration sqlite db with Ef core for blazor hybrid MAUI?

sblb 1,231 Reputation points
2024-10-02T10:06:51.8633333+00:00

Hi

I am working on an application for the Windows platform using Blazor Hybrid MAUI and want to create a SQLite database with multiple tables using EF Core for the migration. I have already defined the ApplicationDbContext.cs file and added it to MauiProgram.cs. However, when I try to migrate, I receive the following error message:

PM> add-migration Initial Build started... Build succeeded. Startup project 'MauiAppDT' targets platform 'Android'. The Entity Framework Core Package Manager Console Tools don't support this platform. See https://aka.ms/efcore-docs-pmc-tfms for more information. 

Have you an idea how I can fix this error?

Thanks in advance

applicationDbContext.cs

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
        Database.EnsureCreated();

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source = C:\\App\\maui\\BaseSQLite\\db.sqlite");        
    }

    public DbSet<SuiviBE> SuiviBEs { get; set; }
    public DbSet<ActionItem> ActionItems { get; set; }
    public DbSet<ADT> ADTs { get; set; }
    public DbSet<EcoRep> EcoReps { get; set; }
    public DbSet<TodoListModel> TodoListModels { get; set; }   
    public DbSet<CMMAction> cMMActions { get; set; }
    public DbSet<ProjetModel> ProjetModels { get; set; }
    public DbSet<ActionItemProjet> ActionItemProjets { get; set; }
    public DbSet<Developer> Developers { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);                        

        modelBuilder.Entity<ActionItem>()
            .HasOne(p => p.SuiviBE)
            .WithMany(b => b.ActionItems);

        modelBuilder.Entity<ActionItemProjet>()
            .HasOne(p => p.ProjetModel)
            .WithMany(b => b.ActionItemProjets);

    }
}


In MauiPrograms.cs

...
 builder.Services.AddDbContext<ApplicationDbContext>();
...

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,581 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,226 Reputation points
    2024-10-02T19:05:58.79+00:00

    Be sure your Maui project is a single target of windows only. Remove the any Android and iOS targets.

    If the dbcontext is in a library (best practice) you could make a seperate hosting windows project to run the migrations in.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.