【发布时间】: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