WCF DataService with Entity Framework: TimeSpan support

ぃ、小莉子 提交于 2019-12-07 12:27:13

问题


I am trying to create a WCF Data Service over an Entity Framework Object context that exposes a number of System.TimeSpan properties. However, when I try to access the service, I get the following error: 'The property 'ScheduledDepartureTime' on type 'DepotRoute' is of type 'Time' which is not a supported primitive type.'

I have tried using DataServiceConfiguration.RegisterKnownType(typeof(TimeSpan)) as well as DataServiceConfiguration.EnableTypeAccess(typeof(TimeSpan).FullName) but neither of these seem to make any difference - I still get the error...

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)));

}

Although my context is being generated as a DbContext, I have overriden CreateDataSource to expose the ObjectContext rather than creating the service as DataService...

protected override ObjectContext CreateDataSource() {

    var context = new MercuryContext().ObjectContext;
    context.ContextOptions.ProxyCreationEnabled = false;

    return context;

}

I have however also tried exposing a service based upon a standard EF Model but this too makes no difference. I have even tried it using VS11 Develop Preview - this too cannot expose my properties.

What am I missing? There must be some way to do this.


回答1:


EF can't use the TimeSpan type in a query; you will need to convert the TimeSpan to the corresponding DateTime value that it represents.



来源:https://stackoverflow.com/questions/9454747/wcf-dataservice-with-entity-framework-timespan-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!