【问题标题】:WCF data services (OData), query with inheritance limitation?WCF 数据服务 (OData),具有继承限制的查询?
【发布时间】:2011-01-16 17:48:25
【问题描述】:

项目:使用内部 EF4 CTP5 代码优先方法的 WCF 数据服务。

我为实体配置了继承 (TPH)。 请参阅有关此主题的上一个问题:

Previous question about multiple entities- same table

映射运行良好,通过 EF4 进行的单元测试确认查询运行顺利。

我的实体如下所示:

  • ContactBase(摘要)
  • 客户(从 ContactBase 继承),此实体还具有多个指向其他实体的导航属性
  • 资源(从 ContactBase 继承)

我已经配置了一个鉴别器,因此 Customer 和 Resource 都映射到同一个表。 同样,从 Ef4 的角度来看,一切都很好(单元测试全是绿色!)

但是,当通过 WCF 数据服务公开此 DBContext 时,我得到: - CustomerBases 集暴露(客户和资源集似乎隐藏,是设计使然吗?) - 当我查询有关客户的 Odata 时,我收到此错误:

Navigation Properties are not supported on derived entity types. Entity Set 'ContactBases' has a instance of type 'CodeFirstNamespace.Customer', which is an derived entity type and has navigation properties. Please remove all the navigation properties from type 'CodeFirstNamespace.Customer'. 

堆栈跟踪:

at System.Data.Services.Serializers.SyndicationSerializer.WriteObjectProperties(IExpandedResult expanded, Object customObject, ResourceType resourceType, Uri absoluteUri, String relativeUri, SyndicationItem item, DictionaryContent content, EpmSourcePathSegment currentSourceRoot)
at System.Data.Services.Serializers.SyndicationSerializer.WriteEntryElement(IExpandedResult expanded, Object element, ResourceType expectedType, Uri absoluteUri, String relativeUri, SyndicationItem target)
at System.Data.Services.Serializers.SyndicationSerializer.<DeferredFeedItems>d__b.MoveNext()
at System.ServiceModel.Syndication.Atom10FeedFormatter.WriteItems(XmlWriter writer, IEnumerable`1 items, Uri feedBaseUri)
at System.ServiceModel.Syndication.Atom10FeedFormatter.WriteFeedTo(XmlWriter writer, SyndicationFeed feed, Boolean isSourceFeed)
at System.ServiceModel.Syndication.Atom10FeedFormatter.WriteFeed(XmlWriter writer)
at System.ServiceModel.Syndication.Atom10FeedFormatter.WriteTo(XmlWriter writer)
at System.Data.Services.Serializers.SyndicationSerializer.WriteTopLevelElements(IExpandedResult expanded, IEnumerator elements, Boolean hasMoved)
at System.Data.Services.Serializers.Serializer.WriteRequest(IEnumerator queryResults, Boolean hasMoved)
at System.Data.Services.ResponseBodyWriter.Write(Stream stream)

似乎是 WCF 数据服务的限制……是吗? 在网上找不到太多关于 WCF 数据服务 (OData) 和继承规范的文档。

我怎样才能超越这个异常?我需要派生实体上的这些导航属性,而继承似乎是使用 Ef4 CTP5 在同一个表上提供 2 个实体的映射的唯一方法......

有什么想法吗?

【问题讨论】:

  • OData 确实很烂。但未来可能会变得更好。 2013 年使用它的糟糕经历。

标签: entity-framework inheritance entity-framework-4 odata wcf-data-services


【解决方案1】:

目前,WCF 数据服务不支持派生类型的导航属性。这是一个已知限制,我们目前正在努力为我们的下一个版本启用此功能。

目前,解决方法是将所有导航属性设置为集合所引用的类型。在您的示例中,您能否将实体集类型设为客户,而不是 ContactBase?

希望这会有所帮助。 谢谢 普拉提克

【讨论】:

  • 这当然有帮助,但也很糟糕。我没有 OData。
  • 派生类型的更新已添加到 WCF 数据服务。 blogs.msdn.com/b/astoriateam/archive/2012/04/09/…
  • 我也想返回一个派生类型,我们在导航中声明父类型。这似乎是 2010 年以来的问题......现在是 2017 年......这个问题解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多