问题
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