使用EntityFramework和.NET Native

时间:2015-11-25 20:49:44

标签: c# .net reflection uwp .net-native

我有一个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属性。 ---&GT; System.PlatformNotSupportedException:   返回true的类型不支持TypeHandles   ContainsGenericParameters。在   Internal.Reflection.Core.NonPortable.RuntimeType.get_TypeHandle()in   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.CoreLib \内部\反射\核心\非便携式\ RuntimeType.cs:线   262点   System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.IsAssignableFrom(所属类别   typeInfo)in   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Reflection.Core \ SYSTEM \反射\运行\ TypeInfos \ RuntimeTypeInfo.cs:线   398在   Remotion.Linq.Parsing.Structure.IntermediateModel.SupportedMethodSpecifications.HasIndexSelectorParameter(MethodInfo的   methodInfo,Int32 parameterPosition)at   Remotion.Linq.Parsing.Structure.IntermediateModel.SupportedMethodSpecifications&LT;&GT; c__DisplayClass14.b__13(MethodInfo的   mi)at System.Func 2.Invoke(T arg) at System.Linq.Enumerable.<>c__DisplayClass0 1.b__1(TSource   x)in   F:\ DD \ NDP \ fxcore \打开\ SRC \ System.Linq的\ SRC \ SYSTEM \ LINQ的\ Enumerable.cs:线   69.在System.Func 2.Invoke(T arg) at System.Linq.Enumerable.WhereEnumerableIterator 1.MoveNext()in   F:\ DD \ NDP \ fxcore \打开\ SRC \ System.Linq的\ SRC \ SYSTEM \ LINQ的\ Enumerable.cs:线   199点   Remotion.Linq.Parsing.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()at at   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()in   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Threading \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   320 at   TIKSN.GroceryChecklist.ModernMobile.ViewModels.ChecklistPageViewModel.d__15.MoveNext()   在E:\ Visual Studio Online \ tiksn \ Grocery中   清单\开发\ ModernMobile.UWP \的ViewModels \ ChecklistPageViewModel.cs:行   82点   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()in   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.CoreLib \ SYSTEM \运行\ ExceptionServices \ ExceptionDispatchInfo.cs:线   66点   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Threading \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   186点   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Threading \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   155在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务   任务)   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Threading \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   127.在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中   F:\ DD \ NDP \ fxcore \ SRC \ System.Private.Threading \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   112点   TIKSN.GroceryChecklist.ModernMobile.ViewModels.PageViewModelBase.d__46.MoveNext()   在E:\ Visual Studio Online \ tiksn \ Grocery中   清单\开发\ ModernMobile.UWP \的ViewModels \ PageViewModelBase.cs:行   102} System.Exception {System.TypeInitializationException}

2 个答案:

答案 0 :(得分:3)

这不是超级启发,但你可以在这里查看例外的来源:https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeType.cs#L262

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

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

所以我认为你可能只需要删除EF中遇到此问题的任何内容。对不起,我没有更好的消息。

答案 1 :(得分:1)

假设您使用的是ef7

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

<!-- Add your application specific runtime directives here. -->
<Type Name="System.Collections.ArrayList" Dynamic="Required All" />