gpt4 book ai didi

c# - 不支持输入类型为 'TypeIs' 且检查类型为 'Domain.Entities.Request' 的 'Domain.Entities.Base' 表达式

转载 作者:太空狗 更新时间:2023-10-29 23:07:53 25 4
gpt4 key购买 nike

我收到这个错误:

The 'TypeIs' expression with an input of type 'Domain.Flood.Entities.Things.SomeObject' and a check of type 'Domain.Entities.Base' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.

我正在尝试调用我的 OData API 并在我的对象上展开 CreatedBy 属性。 CreatedBy Property 在基类中,是一种 User。所以我的基类看起来像这样:

[DataContract(Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public abstract class Base
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DataMember]
public int Id { get; set; }

[DataMember]
public string Name { get; set; }

[DataMember]
public string Description { get; set; }

private User createdBy;
[DataMember]
public virtual User CreatedBy
{
get
{
return createdBy;
}
set
{
createdBy = value;
}
}
}

当我尝试获取从基类继承的小部件,并且我还想包含名为 CreatedBy 的用户对象时,出现此错误。如果我不展开 CreatedBy 对象,我就不会收到错误。这是奇怪的部分,我有另一个 User 属性不在基类中,而是在我的 SomeObject 类中,称为 AssignedTo。如果我对此进行扩展,一切都是美好的。因此,在基类上展开 User 对象,没有问题。将 User 对象向上扩展几层,bueno。

     {
Container.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequest);
ViewBag.Data = Container.SomeObject.Expand("Address, CreatedBy").ToList();
return View();
}

这是堆栈跟踪:

Exception Details: System.Data.Services.Client.DataServiceClientException: <?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code />
<m:message xml:lang="en-US">An error has occurred.</m:message>
<m:innererror>
<m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace></m:stacktrace>
<m:internalexception>
<m:message>The 'TypeIs' expression with an input of type 'Domain.Flood.Entities.Things.SomeObject' and a check of type 'Domain.Entities.Base' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.</m:message>
<m:type>System.NotSupportedException</m:type>
<m:stacktrace> at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.GetIsOrAsTargetType(ExpressionType operationType, Type toClrType, Type fromClrType)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.IsTranslator.TypedTranslate(ExpressionConverter parent, TypeBinaryExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConditionalTranslator.TypedTranslate(ExpressionConverter parent, ConditionalExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding&amp; binding)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression&amp; source, DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&#xD;
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()&#xD;
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)&#xD;
at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;&gt;c__DisplayClassb.&lt;GetResults&gt;b__a()&#xD;
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)&#xD;
at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;&gt;c__DisplayClassb.&lt;GetResults&gt;b__9()&#xD;
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)&#xD;
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)&#xD;
at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator&gt;b__0()&#xD;
at System.Lazy`1.CreateValue()&#xD;
at System.Lazy`1.LazyInitValue()&#xD;
at System.Lazy`1.get_Value()&#xD;
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()&#xD;
at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)&#xD;
at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)&#xD;
at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)&#xD;
at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext)&#xD;
--- End of stack trace from previous location where exception was thrown ---&#xD;
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)&#xD;
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()&#xD;
at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__14.MoveNext()</m:stacktrace>
</m:internalexception>
</m:innererror>
</m:error>

阅读错误消息,我可以看到 Linq to Entities 正在尝试将一件事转换为另一件事,但我真的不知道这是在幕后。

我注意到的另一件事是我的 OData 元数据显示了所有内容,而不仅仅是具有数据契约(Contract)的内容。它过去只包含具有数据契约的类。这很奇怪吧?我通过我的源代码控制查看最近几个变更集中发生了什么变化,但我没有看到与我的基类、用户类或我的 SomeObject 类有任何关系。也与我的 API 无关。

如果我能更清楚一点,请告诉我。我觉得我遗漏了一些东西。

最佳答案

如果你仔细阅读错误:

The 'TypeIs' expression with an input of type 'Domain.Flood.Entities.Things.SomeObject' and a check of type 'Domain.Entities.Base' is not supported.

这意味着 OData 会尝试一个查询,使 EF 生成一个具有“TypeIs”函数的查询,该函数本身很好,但 TypeIs 的两个操作数都必须是实体类型或复杂类型:

Only entity types and complex types are supported in LINQ to Entities queries.

也就是说,在 EF DbContext 中,使用 ModelBuilder,您必须确保上下文已知 SomeObject 和 Base 为 Entity 类型或 Complex 类型。这是用 Fluent API 做的:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Base>(); //EF should crawl and configure all derived types as well.
}

另外,在使用 DbModelBuilder.Ignore() 时要特别注意不要忽略任何重要的类型。

更新:

您还可以使用 ODataConventionModelBuilder 告诉 OData 将所有父类(super class)继承的属性视为继承类型自己的属性:

使用System.Web.OData:

modelBuilder.EntityType<SomeObject>().DerivesFromNothing();

使用System.Web.Http.OData:

modelBuilder.Entity<SomeObject>().DerivesFromNothing();

关于c# - 不支持输入类型为 'TypeIs' 且检查类型为 'Domain.Entities.Request' 的 'Domain.Entities.Base' 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21126471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com