.NET 8 migration on .NET MAUI Blazor Hybrid App causes exception: The type initializer for "<Module>" threw an exception.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
## .NET 8 migration on .NET MAUI Blazor Hybrid App causes exception `Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The type initializer for '' threw an exception.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728`
### My code
#### ApplicationDbContext.cs
```C#
using MauiApp1.Model;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
namespace MauiApp1.Context
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext() { }
public ApplicationDbContext(DbContextOptions contextOptions)
: base(contextOptions)
{
}
public DbSet People { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
}
```
#### MauiProgram.cs
```C#
using MauiApp1.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace MauiApp1
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.Services.AddMauiBlazorWebView();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
builder.Services.AddDbContext(
options =>
options.UseSqlite(
"Data Source=C:\\Users\\USER\\Documents\\Test.db;Cache=Shared"
)
);
builder.Services.AddScoped();
return builder.Build();
}
}
}
```
#### Person.cs
```C#
namespace MauiApp1.Model
{
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
```
#### PersonConfiguration.cs
```C#
using MauiApp1.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace MauiApp1.EntityConfiguration
{
class PersonConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.HasKey(o => o.Id);
builder.Property(t => t.FirstName).IsRequired(false);
builder.Property(t => t.LastName).IsRequired(false);
}
}
}
```
#### MauiApp1.csproj
```xml
all
runtime; build; native; contentfiles; analyzers; buildtransitive
```
### Stack traces - Vebose output
By runing the command: `dotnet ef migrations add FirstMigration --project MauiApp1 --context ApplicationDbContext --verbose` the following error occurs:
```
Using assembly 'MauiApp1'.
Using startup assembly 'MauiApp1'.
Using application base 'C:\Users\USER\source\repos\MauiApp1\MauiApp1\bin\Debug\net8.0-windows10.0.19041.0\win10-x64'.
Using working directory 'C:\Users\USER\source\repos\MauiApp1\MauiApp1'.
Using root namespace 'MauiApp1'.
Using project directory 'C:\Users\USER\source\repos\MauiApp1\MauiApp1\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'MauiApp1'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The type initializer for '' threw an exception.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.TypeInitializationException: The type initializer for '' threw an exception.
---> System.TypeInitializationException: The type initializer for 'WinRT.ActivationFactory`1' threw an exception.
---> System.Runtime.InteropServices.COMException (0x80040154): Interfaccia non registrata. (0x80040154 (REGDB_E_CLASSNOTREG))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName)
at WinRT.ActivationFactory`1..ctor()
at WinRT.ActivationFactory`1..cctor()
--- End of inner exception stack trace ---
at WinRT.ActivationFactory`1.ActivateInstance[I]()
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentInitializeOptions..ctor()
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.get_Options() in C:\Users\USER\.nuget\packages\microsoft.windowsappsdk\1.3.230724000\include\DeploymentManagerAutoInitializer.cs:line 44
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.AccessWindowsAppSDK() in C:\Users\USER\.nuget\packages\microsoft.windowsappsdk\1.3.230724000\include\DeploymentManagerAutoInitializer.cs:line 30
at .cctor()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass20_5.b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'The type initializer for '' threw an exception.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
```
### Include provider and version information
EF Core version: 8.0.0
Database provider: SqlLite
Target framework: .NET 8.0
Operating system: Windows 10 22H2 Build 19045
IDE: Visual Studio 2022 17.8.1
dotnet-ef CLI: 8.0.0
Contributor guide
Assessment
This issue has not been assessed yet.