WCF 数据服务(OData),具有继承限制的查询?

发布于 2024-10-12 06:45:38 字数 2465 浏览 4 评论 0原文

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

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

有关多个实体的上一个问题- 同一个表

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

我的实体如下所示:

  • ContactBase(抽象)
  • 的导航属性
  • Customer(继承自ContactBase),该实体还具有多个指向其他实体Resource(继承自ContactBase)

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

但是,当通过 WCF 数据服务公开此 DBContext 时,我得到: - CustomerBases 集暴露(客户和资源集似乎隐藏,是设计使然吗?) - 当我查询 Customers 上的 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'. 

Stacktrace:

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 数据服务的限制...是吗? 在 Web 上找不到太多有关 WCF 数据服务 (OData) 和继承规范的文档。

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

有什么想法吗?

Project: WCF Data service using internally EF4 CTP5 Code-First approach.

I configured entities with inheritance (TPH).
See previous question on this topic:

Previous question about multiple entities- same table

The mapping works well, and unit test over EF4 confirms that queries runs smoothly.

My entities looks like this:

  • ContactBase (abstract)
  • Customer (inherits from ContactBase), this entity has also several Navigation properties toward other entities
  • Resource (inherits from ContactBase)

I have configured a discriminator, so both Customer and Resource map to the same table.
Again, everythings works fine on the Ef4 point of view (unit tests all greens!)

However, when exposing this DBContext over WCF Data services, I get:
- CustomerBases sets exposed (Customers and Resources sets seems hidden, is it by design?)
- When I query over Odata on Customers, I get this error:

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'. 

Stacktrace:

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)

Seems like a limitation of WCF Data services... is it?
Not much documentation can be found on the web about WCF Data services (OData) and inheritance specifications.

How can I overpass this exception? I need these navigation properties on derived entities, and inheritance seems the only way to provide mapping of 2 entites on the same table with Ef4 CTP5...

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-10-19 06:45:38

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

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

希望这有帮助。
谢谢
普拉蒂克

Currently, WCF data services does not support navigation properties on derived types. This is a known limitation and we are currently working on enabling this for our next release.

Currently, the work around is that you put all the navigation properties to the type refered by the set. In your example, can you make the entity set type be customer, instead of ContactBase?

Hope this helps.
Thanks
Pratik

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文