With Xamarin.Forms on iOS, using EntityFrameworkCore.Sqlite, I'm getting the following errors on real devices when I submit the app for review, but not on the simulator. It only happens on iOS and not on Android devices.
Exceptions: TypeInitializationException, InvalidOperationException
Messages:
The type initializer for 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' threw an exception.
The type initializer for 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' threw an exception.
Sequence contains no matching element
Stack Trace from crash 1:
System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' threw an exception.
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.get_NodeTypeProvider () <0x101ff5100 + 0x00040> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass15_0`1[TResult].<Execute>b__0 () <0x101ff5220 + 0x0002f> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc] (System.Object cacheKey, System.Func`1[TResult] compiler) <0x101fdada0 + 0x000c3> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult] (System.Object cacheKey, System.Func`1[TResult] compiler) <0x101fdad20 + 0x00033> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult] (System.Linq.Expressions.Expression query) <0x101ff3dc0 + 0x00213> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult] (System.Linq.Expressions.Expression expression) <0x101fdb810 + 0x00053> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Remotion.Linq.QueryableBase`1[T].GetEnumerator () <0x1024aea20 + 0x0005f> in <0a52ab02a5054a9e94c0cd3993f4d33a#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1[TEntity].System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator () <0x1020a67f0 + 0x00023> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.Services.DataManager.GetItems () <0x101d74700 + 0x001a3> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.ViewModels.ItemsViewModel.Refresh () <0x101d6dc60 + 0x0001f> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.Views.ItemsPage..ctor () <0x101d63dd0 + 0x00083> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.Views.MasterDetailPage1+<ListView_ItemSelected>d__13.MoveNext () <0x101d60820 + 0x003ef> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x10129a860 + 0x00028> in <d680a8bf3a2a4c8dbbfde665ba81f6f2#20086da1bcd56fe96419e7e4f67122ec>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) <0x10129d360 + 0x00053> in <d680a8bf3a2a4c8dbbfde665ba81f6f2#20086da1bcd56fe96419e7e4f67122ec>:0
at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () <0x101b683c0 + 0x00027> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at Foundation.NSAsyncActionDispatcher.Apply () <0x101b9e240 + 0x00023> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) <0x101b62f90 + 0x0002b> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x101b62ed0 + 0x000ab> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at HemocytometerSidekick.XF.iOS.Application.Main (System.String[] args) <0x1010e80e0 + 0x00023> in <a8610314f7374ebc97cf9e3b7e1a98b2#20086da1bcd56fe96419e7e4f67122ec>:0
--- End of inner exception stack trace ---
System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' threw an exception.
--- End of inner exception stack trace ---
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) <0x101588e80 + 0x001a8> in <98d8ae0ba2c1456c96653359c4b1be27#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.GetMethod (System.String name, System.Int32 parameterCount, System.Func`2[T,TResult] predicate) <0x101faa5a0 + 0x0014f> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions..cctor () <0x101faa710 + 0x0001f> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
Stack Trace from crash 2:
System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' threw an exception.
at System.Linq.Expressions.MethodCallExpression.Accept (System.Linq.Expressions.ExpressionVisitor visitor) <0x100adee60 + 0x00024> in <98d8ae0ba2c1456c96653359c4b1be27#20086da1bcd56fe96419e7e4f67122ec>:0
at System.Linq.Expressions.ExpressionVisitor.Visit (System.Linq.Expressions.Expression node) <0x100ad8ed0 + 0x00037> in <98d8ae0ba2c1456c96653359c4b1be27#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ParameterExtractingExpressionVisitor.ExtractParameters (System.Linq.Expressions.Expression expression) <0x1015289d0 + 0x00087> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExtractParameters (System.Linq.Expressions.Expression query, Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, System.Boolean parameterize) <0x10150cf00 + 0x000b3> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult] (System.Linq.Expressions.Expression query) <0x10164b4e0 + 0x001bb> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at (wrapper unknown) System.Object:gsharedvt_in ()
at (wrapper unknown) System.Object:gsharedvt_out ()
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult] (System.Linq.Expressions.Expression expression) <0x101646160 + 0x000ab> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at (wrapper unknown) System.Object:gsharedvt_in ()
at System.Linq.Queryable.Count[TSource] (System.Linq.IQueryable`1[T] source) <0x100a9bee0 + 0x000bb> in <98d8ae0ba2c1456c96653359c4b1be27#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.Services.DataManager.get_ItemsCount () <0x10128c590 + 0x0005b> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at MyApp.XF.Commands.NewItemCommand+<Execute>d__0.MoveNext () <0x1012ae180 + 0x0006b> in <059607b23bea46f9ab8cce537af6578c#20086da1bcd56fe96419e7e4f67122ec>:0
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x1007b2860 + 0x00028> in <d680a8bf3a2a4c8dbbfde665ba81f6f2#20086da1bcd56fe96419e7e4f67122ec>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) <0x1007b5360 + 0x00053> in <d680a8bf3a2a4c8dbbfde665ba81f6f2#20086da1bcd56fe96419e7e4f67122ec>:0
at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () <0x1010803c0 + 0x00027> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at Foundation.NSAsyncActionDispatcher.Apply () <0x1010b6240 + 0x00023> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) <0x10107af90 + 0x0002b> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10107aed0 + 0x000ab> in <e0596b82250c450abdf075bc558add28#20086da1bcd56fe96419e7e4f67122ec>:0
at HemocytometerSidekick.XF.iOS.Application.Main (System.String[] args) <0x1006000e0 + 0x00023> in <a8610314f7374ebc97cf9e3b7e1a98b2#20086da1bcd56fe96419e7e4f67122ec>:0
--- End of inner exception stack trace ---
System.InvalidOperationException: Sequence contains no matching element
at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) <0x100aa0e80 + 0x001a8> in <98d8ae0ba2c1456c96653359c4b1be27#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.GetMethod (System.String name, System.Int32 parameterCount, System.Func`2[T,TResult] predicate) <0x1014c25a0 + 0x0014f> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions..cctor () <0x1014c2710 + 0x0001f> in <f7da5a12d702478088613d8aefc8f3d1#20086da1bcd56fe96419e7e4f67122ec>:0
It's unclear what is needed to trigger these crashes, except that they may be happening while the database is empty. Can't be reproduced on the iOS simulator. I don't have a real device to test on personally to narrow it down. pcl-SQLite-net had been working previously.
User code that initiated crash 1:
```c#
using (var db = new MyContext())
foreach (var item in db.Items)
{
var subitems = db.Subitems.Where(s => s.ItemtId == item.Id);
itemViewModels.Add(new ItemViewModel(item, subitems));
}
User code that initiated crash 2:
```c#
using (var db = new ItemsContext())
return db.Items.Count();
EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: iOS 11.2.5
IDE: Visual Studio 2017 15.5.6
Oh, apparently this is: https://github.com/aspnet/EntityFrameworkCore/issues/10207#issuecomment-346340935
Which was moved to https://github.com/xamarin/xamarin-macios/issues/3394
Which says:
This is a workaround for consumers of the EntityFrameworkCore. The right fix would be for EntityFrameworkCore to provide all the required hints to the linker (as part of the assembly) since the library authors are the best placed to know the reflection usage made by their code.
I will close this as a dupe of https://github.com/aspnet/EntityFrameworkCore/issues/10963, which is about the possibility of adding linker hints to EF Core, so that you don't need to do it in your app.
@BioTurboNick the workarounds explained by the Xamarin folks work for you? Otherwise feel free to reopen.
@divega Yes, adding [assembly: Preserve(typeof(System.Linq.Queryable), AllMembers = true)] to my iOS project worked.
@divega Yes, adding
[assembly: Preserve(typeof(System.Linq.Queryable), AllMembers = true)]to my iOS project worked.
This helped me to set Linker Behavior as "Link framework SDKs only" and reduce my app size. However, I'm afraid, I don't really understand this change and whether more such lines of code needs to be added or not.
Most helpful comment
@divega Yes, adding
[assembly: Preserve(typeof(System.Linq.Queryable), AllMembers = true)]to my iOS project worked.