尝试使用Azuren搜索覆盖分区密钥和使用.NET的Azure表存储时获取错误请求

时间:2017-05-25 11:50:56

标签: azure azure-storage azure-table-storage

我正在使用Azuren搜索和Azure表存储,并使用.net,我正在尝试索引表并使分区键可过滤现在这工作正常,直到我尝试在该表中插入一些我得到BadRequest的东西更多信息。 这是我的班级吼叫

using System;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.WindowsAzure.Storage.Table;

      [SerializePropertyNamesAsCamelCase]
    public class Asset : TableEntity
    {
        public Asset(string name)
        {
            Name = name;
        }

        public Asset()
        {

        }

        public Asset(string name, DateTimeOffset toBePublished, string pkey)
        {
            Name = name;
            ToBePublishedDate = toBePublished;
            PartitionKey = pkey;
        }

        [System.ComponentModel.DataAnnotations.Key]
        public string Id { get; set; } = DateTimeOffset.UtcNow.ToString("O")
            .Replace("+", string.Empty)
            .Replace(":", string.Empty)
            .Replace(".", string.Empty);

        [IsFilterable, IsSortable, IsSearchable]
        public new string PartitionKey { get; set; }

        [IsFilterable, IsSortable, IsSearchable]
        public string Name { get; set; } = "TemptAsset " + new Guid();

        [IsFilterable, IsSortable]
        public int? Version { get; set; } = 1;

        [IsFilterable, IsSortable]
        public DateTimeOffset? ToBePublishedDate { get; set; } = DateTimeOffset.UtcNow;

        [IsFilterable, IsSortable]
        public DateTimeOffset? ToBeRetiredDate { get; set; } = null;

        [IsFilterable, IsSearchable, IsSortable]
        public string Company { get; set; } = "TempCompany";
        [IsFilterable, IsSortable]
        public bool IsApproved { get; set; } = false;

        [IsFilterable, IsSortable]
        public bool IsDraft { get; set; } = true;


    }

运行并成功创建索引,请参阅下面的内容 enter image description here

现在,如果我尝试向该表添加一个实体,我会得到一个BadRequest,但是在我的实体中注释掉PartitionKey时做同样的事情,这样可以正常工作。 这就是我创建索引的方式

AzureSearch.CreateAssetNameIndex(AzureSearch.CreateSearchServiceClient());

    and the methods called bellow




using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using AssetSynch.Models;
    using Microsoft.Azure.Search;
    using Microsoft.Azure.Search.Models;
      public static SearchServiceClient CreateSearchServiceClient()
        {
            string searchServiceName = "*****";
            string adminApiKey = "********";

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName,
                new SearchCredentials(adminApiKey));

            return serviceClient;
        }

    public static async void CreateAssetNameIndex(SearchServiceClient serviceClient)
        {
            Index definition = new Index
            {
                Name = "assetname",
                Fields = FieldBuilder.BuildForType<Asset>()
            };

            await serviceClient.Indexes.CreateAsync(definition);
        }

如果我使用邮递员返回错误,这是我得到的例外

      {
  "innerExceptions": [
    {
      "requestInformation": {
        "httpStatusCode": 400,
        "httpStatusMessage": "Bad Request",
        "serviceRequestID": "59efbc9a-0002-002c-3570-d5d55c000000",
        "contentMd5": null,
        "etag": null,
        "requestDate": "Thu, 25 May 2017 17:05:01 GMT",
        "targetLocation": 0,
        "extendedErrorInformation": {
          "errorCode": "PropertiesNeedValue",
          "errorMessage": "The values are not specified for all properties in the entity.\nRequestId:59efbc9a-0002-002c-3570-d5d55c000000\nTime:2017-05-25T16:05:06.5197909Z",
          "additionalDetails": {}
        },
        "isRequestServerEncrypted": false
      }
    }
  ]
}

如果我从我的实体中删除分区键并重新运行相同的代码来重新创建索引,则成功执行相同的代码。 我注意到的是,我的实体现在有2个分区键,其中一个将保持为空,请参阅图像以及我的属性不会覆盖原始实体。

enter image description here

我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:1)

根据您的代码,我发现您的资产已使用new关键字来修改基类的分区属性。

但是这只会隐藏base.partition而不是覆盖它。

public new string PartitionKey { get; set; }

在Asset类中设置值后,您会发现它包含两个分区,如下所示:

enter image description here

因此,如果基类的分区键值为null,则返回400错误。

因此,如果要将新实体添加到表中,则需要设置基类(TableEntity)分区键值。

所以我建议您可以更改以下资产:

  [SerializePropertyNamesAsCamelCase]
    public class Asset : TableEntity
    {
        public Asset(string name)
        {
            Name = name;
            base.PartitionKey = this.PartitionKey;
        }

        public Asset()
        {
            base.PartitionKey = this.PartitionKey;
        }

        public Asset(string name, DateTimeOffset toBePublished, string pkey)
        {

            Name = name;
            ToBePublishedDate = toBePublished;         
            PartitionKey = pkey;
            base.PartitionKey = this.PartitionKey;
        }

        [Key]
        [IsFilterable]
        public string Id { get; set; } = DateTimeOffset.UtcNow.ToString("O")
            .Replace("+", string.Empty)
            .Replace(":", string.Empty)
            .Replace(".", string.Empty);

        [IsFilterable, IsSortable, IsSearchable]
        public new string PartitionKey { get; set; }

        [IsFilterable, IsSortable, IsSearchable]
        public string Name { get; set; } = "TemptAsset " + new Guid();

        [IsFilterable, IsSortable]
        public int? Version { get; set; } = 1;

        [IsFilterable, IsSortable]
        public DateTimeOffset? ToBePublishedDate { get; set; } = DateTimeOffset.UtcNow;

        [IsFilterable, IsSortable]
        public DateTimeOffset? ToBeRetiredDate { get; set; } = null;

        [IsFilterable, IsSearchable, IsSortable]
        public string Company { get; set; } = "TempCompany";
        [IsFilterable, IsSortable]
        public bool IsApproved { get; set; } = false;

        [IsFilterable, IsSortable]
        public bool IsDraft { get; set; } = true;


    }

如果您想将表存储用作数据源,我建议您参考此article