使用EntityFramework和.NET Native时出现PlatformNotSupportedExceptio

本文关键字:PlatformNotSupportedExceptio Native EntityFramework NET 使用 | 更新日期: 2023-09-27 18:24:41

我有一个UWP(Windows 10)应用程序,它可以在调试模式下完美运行(禁用.NET Native)。

当我在发布模式下运行它时,(或在调试模式下使用.NET Native编译时,我在返回context.Set().ToList()时收到错误行。

    public IEnumerable<TMobileEntity> ReadAll()
    {
        using (var context = new DataContext(database.DatabasePath))
        {
            return context.Set<TMobileEntity>().ToList();
        }
    }

这是的例外详细信息

{System.TypeInitializationException:类型初始值设定项引发了例外要确定哪种类型,请检查InnerException的StackTrace属性。--->System.PlatformNotSupportedException:对于返回true的类型,不支持TypeHandles包含通用参数。在中的Internal.Reflection.Core.NonPortable.RuntimeType.get_TypeHandle()f: ''dd''ndp''fxcore''src''System.Private.CoreLib''Internal''Reflection''Core''NonPortable''RuntimeType.cs:line262 atSystem.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.IsAssignableFrom(TypeInfotypeInfo)f: ''dd''ndp''fxcore''src''System.Private.Reflection.Core''System''Reflection''RuntimeTypeInfo.cs:line398 atRemotion.Linq.解析.结构.中介模型.支持的方法规范.HasIndexSelectorParameter(MethodInfomethodInfo,Int32 parameterPosition)Remotion。Linq.解析。结构。中介模型。支持的方法规范。<>c_DisplayClass14.b_13(方法信息mi)在System.Func 2.Invoke(T arg) at System.Linq.Enumerable.<>c__DisplayClass0 1.b_1(TSourcex) 在f: ''dd''ndp''fxcore''Open''src''System.Linq''src''Systems''Linq''Enumerable.cs:line69位于中的System.Func 2.Invoke(T arg) at System.Linq.Enumerable.WhereEnumerableIterator 1.MoveNext()f: ''dd''ndp''fxcore''Open''src''System.Linq''src''Systems''Linq''Enumerable.cs:line199 atRemotion.Linq.Passing.Structure.NodeTypeProviders.MethodInfoBasedNodeTypeRegistry.Register(IEnumerable 1 methods, Type nodeType) at Remotion.Linq.Parsing.Structure.NodeTypeProviders.MethodInfoBasedNodeTypeRegistry.CreateFromRelinqAssembly() at Microsoft.Data.Entity.Query.QueryCompiler.CreateNodeTypeProvider() at Microsoft.Data.Entity.Query.QueryCompiler..cctor() at System.Runtime.CompilerServices.ClassConstructorRunner.Call[T](IntPtr pfn) at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(Void* returnValue, StaticClassConstructionContext* pContext) in f:'dd'ndp'fxcore'src'System.Private.CoreLib'System'Runtime'CompilerServices'ClassConstructorRunner.cs:line 69 Exception_EndOfInnerExceptionStack at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(Void* returnValue, StaticClassConstructionContext* pContext) in f:'dd'ndp'fxcore'src'System.Private.CoreLib'System'Runtime'CompilerServices'ClassConstructorRunner.cs:line 86 at Microsoft.Data.Entity.Query.QueryCompiler.Preprocess(Expression query, QueryContext queryContext) at Microsoft.Data.Entity.Query.QueryCompiler.Execute[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase 1.GetEnumerator(),位于Microsoft.Data.Entity.Internal.InternalDbSet 1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator() at System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) in f:'dd'ndp'fxcore'src'System.Collections'System'Collections'Generic'List.cs:line 88 at System.Linq.Enumerable.ToList[TSource](IEnumerable 1源)在TIKSN.GroceryChecklist.ModernMobile.Data.RepositoryBase 1.ReadAll() in E:'Visual Studio Online'tiksn'Grocery Checklist'Develop'ModernMobile.Data.UWP'RepositoryBase.cs:line 42
at TIKSN.GroceryChecklist.MobileDataService.ChecklistItemsManagementService.<GetCurrentChecklistItems>d__5.MoveNext() in E:'Visual Studio Online'tiksn'Grocery Checklist'Develop'MobileDataService'ChecklistItemsManagementService.cs:line 62 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:'dd'ndp'fxcore'src'System.Private.CoreLib'System'Runtime'ExceptionServices'ExceptionDispatchInfo.cs:line 66 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in f:'dd'ndp'fxcore'src'System.Private.Threading'System'Runtime'CompilerServices'TaskAwaiter.cs:line 186 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in f:'dd'ndp'fxcore'src'System.Private.Threading'System'Runtime'CompilerServices'TaskAwaiter.cs:line 155 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) in f:'dd'ndp'fxcore'src'System.Private.Threading'System'Runtime'CompilerServices'TaskAwaiter.cs:line 127 at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()中f: ''dd''ndp''fxcore''src''System.Private.Threading''System''Runtime''CompilerServices''TaskAwaiter.cs:line320 atTIKSN.GroceryChecklist.ModernMobile.ViewModels.ChecklistPageViewModel.d_15.MoveNext()在E:''Visual Studio Online''tiksn''杂货店清单''开发'' ModernMobile.UWP''ViewModels''ChecklistPageViewModel.cs:line82中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Sthrow()f: ''dd''ndp''fxcore''src''System.Private.CoreLib''System''Runtime''ExceptionServices''ExceptionDispatchInfo.cs:line66System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)f: ''dd''ndp''fxcore''src''System.Private.Threading''System''Runtime''CompilerServices''TaskAwaiter.cs:line186 atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)f: ''dd''ndp''fxcore''src''System.Private.Threading''System''Runtime''CompilerServices''TaskAwaiter.cs:line155在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)f: ''dd''ndp''fxcore''src''System.Private.Threading''System''Runtime''CompilerServices''TaskAwaiter.cs:line127位于中的System.Runtime.CompilerServices.TaskAwaiter.GetResult()f: ''dd''ndp''fxcore''src''System.Private.Threading''System''Runtime''CompilerServices''TaskAwaiter.cs:line112 atTIKSN.GroceryChecklist.ModernMobile.ViewModels.PageViewModelBase.d_46.MoveNext()在E:''Visual Studio Online''tiksn''杂货店清单''开发'' ModernMobile.UWP''ViewModels''PageViewModelBase.cs:line102}System.Exception{System.TypeInitializationException}

使用EntityFramework和.NET Native时出现PlatformNotSupportedExceptio

这不是很有启发性,但你可以在这里查看异常的来源:https://github.com/dotnet/corrert/bob/master/src/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeType.cs#L262

在高水平上,EF确实还没有准备好支持UWP和提前编译。我知道他们正在努力为UWP工具的下一次更新提供支持。

这个特定的问题是,.NET Native中反射的当前实现正试图使用TypeInfo.IsAssignableFrom,并且相对于未实例化的泛型类型有限制。我认为计划是在.NET Native的下一个版本中纠正这一点。

所以我认为你可能只需要删除EF中涉及这个问题的内容。对不起,我没有更好的消息。

假设您使用的是ef7

打开Properties/Default.rd.xml,并将以下内容添加到文件中

<!-- Add your application specific runtime directives here. -->
<Type Name="System.Collections.ArrayList" Dynamic="Required All" />
相关文章:
  • 没有找到相关文章