Azure表存储 - 更新到3.0导致DataServiceQuery错误

时间:2014-01-16 17:03:12

标签: azure wcf-data-services azure-storage azure-table-storage

我最近将Microsoft.WindowsAzure.Storage的nuget包更新为3.0包,其中还包括对WCF数据服务客户端及其依赖项的更新。由于更新,我在查询解决时收到错误:

  

“客户端和服务之间存在类型不匹配   'ShiftDigital.Flow.Data.RouteDiagnostic'不是实体类型,但是   响应有效负载中的类型表示实体类型。请   确保客户端上定义的类型与数据模型匹配   服务,或更新客户端上的服务引用。“

我什么也没做,只是更新了软件包,我的应用程序和我在LinqPad中设置的测试脚本生成了这个异常。

以下是我在更新之前回复的实体的定义

public class RouteDiagnostic : TableEntity
    {
        public long? LeadRecipientRouteId { get; set; }
        public bool Successful { get; set; }
        public int Duration { get; set; }
        public string Request { get; set; }
        public string Response { get; set; }

        public RouteDiagnostic()
            : base()
        {
            this.Timestamp = DateTimeOffset.Now;
            this.PartitionKey = GetPartitionKey(this.Timestamp.Date);
            this.RowKey = Guid.NewGuid().ToString();
        }

        public static string GetPartitionKey(DateTime? keyDateTime = null)
        {
            return string.Format("{0:yyyyyMM}", keyDateTime ?? DateTime.Now);
        }
    }

以下是执行查询的代码

var storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("...");
var tableClient = storageAccount.CreateCloudTableClient();

var tableContext = new Microsoft.WindowsAzure.Storage.Table.DataServices.TableServiceContext(tableClient);

var diagnostics =
        tableContext.CreateQuery<RouteDiagnostic>("RouteDiagnostic")
        .Where(rd => rd.PartitionKey == "0201401")
        .ToList();

在使用数据服务查询时,最新更新中的内容是否已更改,或者以不同方式构造实体?

2 个答案:

答案 0 :(得分:12)

随着对WCF数据服务5.6的更新,我需要将以下属性添加到我的类型中:

[DataServiceKey("PartitionKey", "RowKey")]

一旦我添加了DataServiceKey属性,一切都很好。

答案 1 :(得分:2)

使用WCF数据服务时,请使您的类继承自TableServiceEntity而不是已经定义了DataServiceKey属性的TableEntity。 TableEntity用于Windows Azure存储客户端库中的新表服务层。有关新表服务层的更多信息,请参阅我们的blog post