asp.net - 具有 Entity Framework 的 WCF 数据服务 : TimeSpan support

标签 asp.net .net wcf-ria-services wcf-data-services

我正在尝试在公开许多 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/

相关文章:

c# - 如何用 Mono 引用这些包以便编译

silverlight - 为什么 Entity Framework 在应该更新的时候却插入了?

silverlight - WCF RIA服务中的业务逻辑位置

javascript - 将 JavaScript 变量传递给辅助方法

c# - 获取 ASP.NET 的通用方法未选中复选框列表或单选按钮列表中的项目?

c# - Form.Show方法错误C#

c# - C#中如何抓取TCP数据包

c# - 选择使用 IIS 托管 WCF 服务或使用 Windows 服务托管自托管

javascript - 无法加载资源: the server responded with a status of 405 (Method Not Allowed)

c# - 在应用程序池中运行我的邮件队列是否错误?