efcore: System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception.

File a bug

During the upgrade of a NuGet package, I find myself unable to run my app anymore because of this exception. I am unable to provide a working reproducable sample but I am mostly looking for guidance on what troubleshooting steps I can take to resolve this issue, since it is most probably related to the configuration.

The app in question is a Uno Platform app, and contains a subproject for processing an iOS intent.

Both the iOS and intents project are using

  • Microsoft.EntityFrameworkCore 5.0.7
  • Microsoft.EntityFrameworkCore.Sqlite 5.0.7

Stack traces

System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception. ---> System.Collections.Generic.KeyNotFoundException: The given key 'All' was not present in the dictionary.
  at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x0001e] in <da8885cdf78b449d96de00cdb9d47225>:0 
  at Microsoft.EntityFrameworkCore.Query.QueryableMethods.<.cctor>g__GetMethod|185_58 (System.String name, System.Int32 genericParameterCount, System.Func`2[T,TResult] parameterGenerator, Microsoft.EntityFrameworkCore.Query.QueryableMethods+<>c__DisplayClass185_0& ) [0x00014] in <4d1f22249bbe4676a871766038953138>:0 
  at Microsoft.EntityFrameworkCore.Query.QueryableMethods..cctor () [0x00052] in <4d1f22249bbe4676a871766038953138>:0 
   --- End of inner exception stack trace ---
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryableMethodNormalizingExpressionVisitor.VisitMethodCall (System.Linq.Expressions.M<…>

Include provider and version information

EF Core version: 5.0.7 Database provider: Microsoft.EntityFrameworkCore.Sqlite Target framework: Not sure Operating system: MacOS Big Sur 11.4 IDE: Visual Studio for Mac 8.10.4 (build 11)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 20 (8 by maintainers)

Most upvoted comments

Hi, I’ve been able to resolve this issue by telling the linker to skip the System.Core assembly (as the EF Core documentation says).

I’ve set the Linking behavior to SDK Only and added this to the Additional mtouch arguments:

--linkskip System.Core

Until we address issue #21894, we recommend disabling the linker entirely:

<PropertyGroup>
  <PublishTrimmed>False</PublishTrimmed>
</PropertyGroup>

We ran into this issue with Xamarin but only in iOS Release configuration. Our workaround is to specify some linking skip settings in the iOS Build properties under “Additional mtouch arguments”

–optimize=experimental-xforms-product-type --linkskip=System.Core --linkskip=mscorlib

Is there any update on this bug?

Are there any updates on this bug yet as I am currently stuck?

Since I get exactly the same error I assume the author is also using Xamarin in combination with EF Core / SQLite.

If more info is needed, I have simplified a method from my project and pasted it here. When I test it on a simulator or real device I get no errors. As soon as I load the app into the app store and test it via Testflight I get the above error.


        private static IEnumerable<T> GetFooKey<T>(int keyKind, Expression<Func<KeyUsage, bool>> filter = null) where T : class, new()
        {
            using (MobileDbContext ctx = GetMobileDbContext())
            {
                IQueryable<KeyUsage> query = ctx.KeyUsage.Where(use => use.KeyKind == keyKind);
                if (filter != null)
                {
                    query = query.Where(filter);
                }

                List<T> fooList = new List<T>();
                foreach (KeyUsage itemKey in query.OrderByDescending(use => use.UsageCount).Take(20)?.ToList())
                {
                    T item = ctx.Find<T>(itemKey.Id);
                    if (item is null)
                    {
                        ctx.KeyUsage.Remove(itemKey);
                    }
                    else
                    {
                        fooList.Add(item);
                    }
                }

                ctx.SaveChanges();
                return fooList;
            }
        }

Nuget Packages (Microsoft.EntityFrameworkCore / 3.1.19) (Microsoft.EntityFrameworkCore.Sqlite / 3.1.19) And also: (Microsoft.EntityFrameworkCore / 5.0.11) (Microsoft.EntityFrameworkCore.Sqlite / 5.0.11)

Sounds good. I will reach out with some details.

Having same issue.

System.Collections.Generic.KeyNotFoundException: The given key ‘All’ was not present in the dictionary.

It happens when I call DbSet<TItem>.ToList() only on iOS.

I followed source code and I can not get it because it should work on both platform, Android and iOS but only Android works.

I can find a method named All in Queryable. But why reflection can not find that only in iOS?

https://github.com/dotnet/efcore/blob/59ec671ecf9bd78d7870757e909f16d100e59bc8/src/EFCore/Query/QueryableMethods.cs#L478-L483

code queryableMethodGroups[name] throws KeyNotFoundException.

https://github.com/dotnet/efcore/blob/59ec671ecf9bd78d7870757e909f16d100e59bc8/src/EFCore/Query/QueryableMethods.cs#L839-L844

dotnet/runtime System.Linq.Queryable

[DynamicDependency("All`1", typeof(Enumerable))]
public static bool All<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
{
    if (source == null)
        throw Error.ArgumentNull(nameof(source));
    if (predicate == null)
        throw Error.ArgumentNull(nameof(predicate));
    return source.Provider.Execute<bool>(
        Expression.Call(
            null,
            CachedReflectionInfo.All_TSource_2(typeof(TSource)),
            source.Expression, Expression.Quote(predicate)
            ));
}

https://github.com/dotnet/runtime/blob/b2a5499230f005108b882ac8bd76b3868d2573e4/src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs#L1500-L1513

Is there any workaround?

What are the previous versions of the packages you upgraded?

Something in this code isn’t working on iOS:

https://github.com/dotnet/efcore/blob/cfe1df8abdd9f3d94347d44b9bdb178510a4bb6b/src/EFCore/Query/QueryableMethods.cs#L473-L476

Possibly caused by too much of the assembly getting trimmed at runtime (see also #10963)