我正在尝试在公开许多 System.TimeSpan 属性的 Entity Framework 对象上下文上创建 WCF 数据服务。但是,当我尝试访问该服务时,出现以下错误:“‘DepotRoute’类型上的属性‘ScheduledDepartureTime’属于‘Time’类型,这不是受支持的原始类型。”
我尝试过使用 DataServiceConfiguration.RegisterKnownType(typeof(TimeSpan)) 以及 DataServiceConfiguration.EnableTypeAccess(typeof(TimeSpan).FullName) 但这些似乎都没有任何区别 - 我仍然收到错误...
public static void InitializeService(DataServiceConfiguration config) {
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
config.RegisterKnownType(typeof(TimeSpan));
config.EnableTypeAccess(typeof(TimeSpan).FullName);
RouteTable.Routes.Add(new ServiceRoute("Data", new DataServiceHostFactory(), typeof(Data)));
}
虽然我的上下文是作为 DbContext 生成的,但我已重写 CreateDataSource 以公开 ObjectContext,而不是将服务创建为 DataService...
protected override ObjectContext CreateDataSource() {
var context = new MercuryContext().ObjectContext;
context.ContextOptions.ProxyCreationEnabled = false;
return context;
}
不过,我也尝试过公开基于标准 EF 模型的服务,但这也没有什么区别。我什至尝试过使用 VS11 开发预览 - 这也无法公开我的属性。
我错过了什么?一定有某种方法可以做到这一点。
最佳答案
EF 不能在查询中使用 TimeSpan 类型;您需要将 TimeSpan 转换为它所代表的相应 DateTime 值。
关于asp.net - 具有 Entity Framework 的 WCF 数据服务 : TimeSpan support,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454747/