How to fix "The type initializer for 'WinRT.ActivationFactory1' threw an exception."

Ubaldo Zambelli 0 Reputation points
2024-09-20T10:38:21.5566667+00:00

Hi there,

I'm developing a mobile application with Net.MAUI, using SQLite db, EntityFrameworkCore, Net8, and I'm not able to create Migrations to manage db schema, using the following command in the Package Manager Console:

Add-Migration INIT -o Data/Migrations

The project builds, but then this error appears and the migration is not created:

Unable to create a 'DbContext' of type ''. The exception 'The type initializer for 'WinRT.ActivationFactory`1' threw an exception.' was thrown while attempting to create an instance

Here the DbContext derived class in my project, and the simple entity class that I'm using for testing

    public class DataContext : DbContext
    {
		public DataContext() {}
        public DbSet<Simple> Simples { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string dbPath = Path.Combine(FileSystem.AppDataDirectory, "simple.db");
            optionsBuilder.UseSqlite($"Filename={dbPath}");
        }
	}

    public class Simple
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; } 
    }

In the MauiProgram class I register the context:

builder.Services.AddDbContext<DataContext>();

In the project settings I've already applied some tricks that have resolved previous errors:

<!-- coment this while creating migrations -->
<!-- <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> -->

<!-- add this to avoid other errors during Add-Migration -->
<WindowsAppSdkDeploymentManagerInitialize>false</WindowsAppSdkDeploymentManagerInitialize>

<!-- set this to avoid other errors during Add-Migration -->
<WindowsPackageType>None</WindowsPackageType>

These the installed packages referring EntityFrameworkCore:

InstalledPackages

Thanks in advance

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
743 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,540 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,371 Reputation points Microsoft Vendor
    2024-09-23T03:14:09.4966667+00:00

    Hello,

    I'm developing a mobile application with Net.MAUI, using SQLite db, EntityFrameworkCore, Net8

    For the combined use of these three types of components, you could refer to this official document for best practices on the operating steps.

    Although this document is for Xamarin, it has been tested and the content of this document is applicable to Maui.

    I'm not able to create Migrations to manage db schema

    The Maui project does not support direct creation of migrations. According to the documentation of Entity Framework Core tools, you need to use an additional Class Library project to create migrations and reference it.

    Other target frameworks The Package Manager Console tools work with .NET Core or .NET Framework projects. Apps that have the EF Core model in a .NET Standard class library might not have a .NET Core or .NET Framework project. For example, this is true of Xamarin and Universal Windows Platform apps. In such cases, you can create a .NET Core or .NET Framework console app project whose only purpose is to act as startup project for the tools. The project can be a dummy project with no real code — it is only needed to provide a target for the tooling. Why is a dummy project required? As mentioned earlier, the tools have to execute application code at design time. To do that, they need to use the .NET Core or .NET Framework runtime. When the EF Core model is in a project that targets .NET Core or .NET Framework, the EF Core tools borrow the runtime from the project. They can't do that if the EF Core model is in a .NET Standard class library. The .NET Standard is not an actual .NET implementation; it's a specification of a set of APIs that .NET implementations must support. Therefore .NET Standard is not sufficient for the EF Core tools to execute application code. The dummy project you create to use as startup project provides a concrete target platform into which the tools can load the .NET Standard class library.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

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.