带有实体框架的WCF DataService:TimeSpan支持

时间:2012-02-26 16:21:29

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

我正在尝试在实体框架对象上下文中创建一个WCF数据服务,该上下文公开了许多System.TimeSpan属性。但是,当我尝试访问该服务时,我收到以下错误:'属性' ScheduledDepartureTime'在类型' DepotRoute'属于'时间'这不是受支持的原始类型。'

我尝试过使用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 Develop Preview - 这也不能暴露我的属性。

我错过了什么?必须有一些方法来做到这一点。

1 个答案:

答案 0 :(得分:0)

EF不能在查询中使用TimeSpan类型;您需要将TimeSpan转换为它所代表的相应DateTime值。

相关问题