问题
Why does the following code of my entity "Person" generates an error in my WCF Data Service:
[System.Data.Services.Common.DataServiceKey("PartitionKey", "RowKey")]
public class Person : TableServiceEntity
{
public string Name { get; set; }
public int Age { get; set; }
... etc
Error:
Request Error
The server encountered an error processing the request. The exception message is 'On data context type 'PersonDataServiceContext', there is a top IQueryable property 'Person' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.'. See server logs for more details. The exception stack trace is:
at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary
2 knownTypes, IDictionary
2 childTypes, IDictionary2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.LoadMetadata() at System.Data.Services.DataService
1.CreateMetadataAndQueryProviders(IDataServiceMetadataProvider& metadataProviderInstance, IDataServiceQueryProvider& queryProviderInstance, BaseServiceProvider& builtInProvider, Object& dataSourceInstance) at System.Data.Services.DataService1.CreateProvider() at System.Data.Services.DataService
1.HandleRequest() at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
What am I doing wrong? If I add a property called PersonID, the error is gone, but I need to access my entity through the PartitionKey and RowKey, and the DataServiceKey decoration in the class is not doing anything.
I'm using Visual Studio 2012, .NET Framework 4.0, Silverlight 5.
回答1:
This appears to be a limitation of the Reflection Provider, and probably not an intended limitation. I'll file this as a bug internally, but here is a workaround until we get to a fix...
You can use the new modifier to hide pass the desired values through/from the base class:
using System;
using System.Data.Services.Common;
namespace SO.OData
{
[DataServiceKey("PartitionKey", "RowKey")]
public class Question : TableServiceEntry
{
public new string PartitionKey
{
get { return base.PartitionKey; }
set { base.PartitionKey = value; }
}
public new string RowKey
{
get { return base.RowKey; }
set { base.RowKey = value; }
}
public string Text { get; set; }
public User AskedBy { get; set; }
public DateTimeOffset AskedAt { get; set; }
}
public abstract class TableServiceEntry
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
}
}
来源:https://stackoverflow.com/questions/12376741/wcf-data-service-and-azure-table-storage-how-can-i-use-partitionkey-rowkey-as