If your DbContext is in an MSIX project, then the use of Add-Migration from PM does not work. Specifically, the MSIX project expects RuntimeIdentifier which is not provided directly by Add-Migration. This can be avoided if using dotnet ef because one can pass the --runtime option to dotnet ef, specifying the RuntimeIdentifier. However Add-Migration does not have this option. Add-Migration also does not have the --no-build option, which would be another way to work around the issue.
The Add-Migration command leads to the error message and stack trace:
Finding DbContext classes in the project...
Using DbContext factory 'AccountingDbContextDesignTimeFactory'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type 'AccountingDbContext'. The exception 'The type initializer for '<Module>' 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 '<Module>' threw an exception.
---> System.Runtime.InteropServices.COMException (0x80040154): Class not registered (0x80040154 (REGDB_E_CLASSNOTREG))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at WinRT.ActivationFactory.Get(String typeName)
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentInitializeOptions.get__objRef_global__Microsoft_Windows_ApplicationModel_WindowsAppRuntime_DeploymentInitializeOptions()
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentInitializeOptions..ctor()
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.get_Options() in C:\Users\steve\.nuget\packages\microsoft.windowsappsdk.foundation\2.1.0\include\DeploymentManagerAutoInitializer.cs:line 41
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.DeploymentManagerCS.AutoInitialize.AccessWindowsAppSDK() in C:\Users\steve\.nuget\packages\microsoft.windowsappsdk.foundation\2.1.0\include\DeploymentManagerAutoInitializer.cs:line 30
at Microsoft.Windows.ApplicationModel.WindowsAppRuntime.Common.AutoInitialize.InitializeWindowsAppSDK() in C:\Users\steve\.nuget\packages\microsoft.windowsappsdk.foundation\2.1.0\include\WindowsAppRuntimeAutoInitializer.cs:line 22
at .cctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.GetActivationInfo(RuntimeType rt, & pfnAllocator, Void*& vAllocatorFirstArg, & pfnRefCtor, & pfnValueCtor, Boolean& ctorIsPublic)
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.ActivatorCache.Create(RuntimeType type)
at System.RuntimeType.IGenericCacheEntry`1.CreateAndCache(RuntimeType type)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass27_1.<FindContextTypes>b__9()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType, KeyValuePair`2 contextPair)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType, KeyValuePair`2 contextPair)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace, Boolean dryRun)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create a 'DbContext' of type 'XxxxxDbContext'. The exception 'The type initializer for '<Module>' 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
Bug description
If your DbContext is in an MSIX project, then the use of Add-Migration from PM does not work. Specifically, the MSIX project expects RuntimeIdentifier which is not provided directly by Add-Migration. This can be avoided if using dotnet ef because one can pass the --runtime option to dotnet ef, specifying the RuntimeIdentifier. However Add-Migration does not have this option. Add-Migration also does not have the --no-build option, which would be another way to work around the issue.
Your code
The problem and various workarounds is described in this post.
Stack traces
Verbose output
EF Core version
NuGet 10.0.9
Database provider
Sqlite
Target framework
.NET 10
Operating system
No response
IDE
No response