使用Linq时应用程序在单声道下崩溃

时间:2016-01-20 17:01:37

标签: c# entity-framework linq mono servicestack

我正在使用 ServiceStack EF6(MySQL)编写一个rest api。代码在windows下运行没有问题,servicestack本身在linux下运行正常(HyperfastCGI4 + Nginx)。
但是,如果我调用使用EntityFramework的服务,我会得到一个NotSupportedException和一个堆栈跟踪,我真的不明白。
我错过了什么吗?

请求结果:

{
"ResponseStatus": {
"ErrorCode": "NotSupportedException",
"Message": "Operation is not supported.",
"StackTrace": "[Authenticate: 1/20/2016 4:34:10 PM]:  
[REQUEST: {UserName:foo@bar.derp,Password:******}]  
System.NotSupportedException: Operation is not supported.  
  at System.Reflection.MonoGenericClass.GetConstructors (BindingFlags bf) [0x00000] in <filename unknown>:0   
  at System.Reflection.TypeInfo.get_DeclaredConstructors () [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Utilities.TypeExtensions.GetDeclaredConstructors (System.Type type) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Utilities.TypeExtensions.GetDeclaredConstructor (System.Type type, System.Type[] parameterTypes) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.CodeGenEmitter.CreateEntityWrapper (System.Linq.Expressions.Expression input, System.Linq.Expressions.Expression keyReader, System.Linq.Expressions.Expression entitySetReader, System.Type actualType, System.Type identityType, MergeOption mergeOption, Boolean isProxy) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.CodeGenEmitter.Emit_EnsureTypeAndWrap (System.Linq.Expressions.Expression input, System.Linq.Expressions.Expression keyReader, System.Linq.Expressions.Expression entitySetReader, System.Type requestedType, System.Type identityType, System.Type actualType, MergeOption mergeOption, Boolean isProxy) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator+TranslatorVisitor.Emit_ConstructEntity (System.Data.Entity.Core.Metadata.Edm.EntityType oSpaceType, IEnumerable`1 propertyBindings, System.Linq.Expressions.Expression entityKeyReader, System.Linq.Expressions.Expression entitySetReader, TranslatorArg arg, System.Data.Entity.Core.Objects.Internal.EntityProxyTypeInfo proxyTypeInfo) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator+TranslatorVisitor.Visit (System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap columnMap, TranslatorArg arg) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TranslatorResult,TranslatorArg] (System.Data.Entity.Core.Query.InternalTrees.ColumnMapVisitorWithResults`2 visitor, TranslatorArg arg) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator+TranslatorVisitor.ProcessCollectionColumnMap (System.Data.Entity.Core.Query.InternalTrees.CollectionColumnMap columnMap, TranslatorArg arg, System.Data.Entity.Core.Query.InternalTrees.ColumnMap discriminatorColumnMap, System.Object discriminatorValue) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator+TranslatorVisitor.ProcessCollectionColumnMap (System.Data.Entity.Core.Query.InternalTrees.CollectionColumnMap columnMap, TranslatorArg arg) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator+TranslatorVisitor.Visit (System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap columnMap, TranslatorArg arg) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TranslatorResult,TranslatorArg] (System.Data.Entity.Core.Query.InternalTrees.ColumnMapVisitorWithResults`2 visitor, TranslatorArg arg) [0x00000] in <filename unknown>:0   
  at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap[User] (System.Data.Entity.Core.Query.InternalTrees.ColumnMap columnMap, System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace workspace, System.Data.Entity.Core.Objects.Internal.SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer) [0x00000] in <filename unknown>:0   
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)  
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 ",
"Errors": []
}
}


修改

通过注释掉从数据库接收一些数据的Linq表达式,我摆脱了上述错误:

var userRow = modelContext.User.FirstOrDefault(user => user.Mail == mail);

我找到了一个调用反射方法并在实体框架源中返回IQueryable的方法,但我不知道这是否与问题有关。至少它引导我朝着正确的方向前进 以下是实施:

/// <summary>
///     Creates an appropriate generic IQueryable using Reflection and the underlying ElementType of
///     the given ObjectQuery.
/// </summary>
private IQueryable CreateQuery(ObjectQuery objectQuery)
{
    var internalQuery = CreateInternalQuery(objectQuery);

    var genericDbQueryType = typeof(InternalDbQuery<>).MakeGenericType(internalQuery.ElementType);
    var constructor = genericDbQueryType.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Single();
    return (IQueryable)constructor.Invoke(new object[] { internalQuery });
}

Linq会导致单声道问题,如果是,有没有办法解决它?

1 个答案:

答案 0 :(得分:2)

重新定位3.5修复问题
哎呀
此外Update-Package -reinstall会为您重新安装所有的内容