我有DbContext子类
ReportingContext : DbContext
当我正在做简单的CRUD时,我创建了一个WCF数据服务来公开我的DbSet ......
public class ReportService : DataService<ReportingContext>
我已经能够直接使用ReportingContext进行'table splitting'。基本上使用两个实体(ReportLayout和ReportLayoutData),它们都使用单个表。我能够使用流畅的API配置它。在Unit测试中一切正常,因为我能够返回ReportLayouts并且只在访问它们时加载ReportLayoutData。
当我尝试使用DataServiceContext类通过WCF数据服务OData版本5.6执行此操作时,我的问题就出现了。返回ReportLayouts工作正常,但到目前为止还没有尝试延迟加载依赖数据。我尝试了不同的东西:
当我直接调试服务时,通过服务方法调用Include实际工作并检查生成的sql - 2单独的查询,就像单元测试一样。但是,在浏览器中查看时,该服务只是在其返回的属性中不包含ReportLayoutData属性,并且我收到了与缺失属性相关的客户端错误。
[WebGet]
public IQueryable<ReportLayout> GetReportsByID(string ids)
{
var ints = GetInts(ids);
return CurrentDataSource.Reports.Include("LayoutData").Where(x => ints.Contains(x.ReportLayoutID)).AsQueryable();
}
private static int[] GetInts(string ids)
{
return ids.Split(",".ToCharArray()).Select(x => Convert.ToInt32(x)).ToArray();
}
我尝试使用DataServiceContext.Expand - $ expand - 这种方法失败了 错误,因为我尝试了不同的论点
我试过调用Execute,各种问题
我将ReportLayoutData属性转换为IQueryable,即使它是1-1关系,现在它说当运行以前工作正常的EF特定单元测试时,ReportLayoutData不是ReportLayout的属性。
我的问题:是否可以通过这种方式通过WCF数据服务进行延迟加载,还是应该公开2个集合并将结果解析为客户端上的单个对象?如果有可能,我只想看看基本模式 - 几个相关实体,流畅的API声明和DataService代码。谢谢你的帮助。
修改
我目前正受到错误的困扰:
类型为“ReportLayout”的名称为“LayoutData”的属性具有类似“结构”的属性,但预计会有类似的“导航”。
虽然在浏览器中检索数据没有问题:ReportService.svc / Reports()?$ expand = LayoutData
部分堆栈跟踪:
Microsoft.Data.OData.ReaderValidationUtils.ValidateNavigationPropertyDefined(String propertyName,IEdmEntityType owningEntityType,ODataMessageReaderSettings messageReaderSettings) 在 Microsoft.Data.OData.Atom.ODataAtomEntryAndFeedDeserializer.TryReadNavigationLinkInEntry(IODataAtomReaderEntryState entryState,String linkRelation,String linkHRef)
我能够通过不通过服务暴露2个dbSet来消除上述错误。将考虑服务操作从EF返回我需要的东西,耻辱它不是那么优雅。
答案 0 :(得分:0)
最后我的解决方案是拆分表,以便创建一个暴露为ICollection的导航属性
我能够实现诸如 Reports / ReportService.svc / Reports(1)$ expand = LayoutData 之类的查询(AddQueryOption(“$ expand”,“LayoutData”)并写了一个服务方法为倍数执行此操作。
[WebGet]
public IQueryable<ReportLayout> GetReportsByID(string ids)
我只通过服务公开了一个Dbset - 儿童无法直接访问。
可以使用DataServiceContext方法实现对从属实体的客户端更新:
AddObject, AddLink
AttachTo, UpdateObject, AttachLink //UpdateObject on the child ensures the entity state changes to modified (see DataServiceContext.Entites collection).
回想起来,我可能不需要拆分桌子,但没有时间玩这个。