尝试使用Azuren搜索和Azure表存储覆盖分区键时,.NET收到错误请求
我正在使用Azuren搜索和Azure表存储以及.net,我正在尝试索引一个表并使分区键可过滤。现在,这一切正常,直到我尝试在该表中插入一些内容,在该表中我得到一个没有太多额外信息的坏请求。 这是我的班级尝试使用Azuren搜索和Azure表存储覆盖分区键时,.NET收到错误请求,azure,azure-storage,azure-table-storage,Azure,Azure Storage,Azure Table Storage,我正在使用Azuren搜索和Azure表存储以及.net,我正在尝试索引一个表并使分区键可过滤。现在,这一切正常,直到我尝试在该表中插入一些内容,在该表中我得到一个没有太多额外信息的坏请求。 这是我的班级 using System; using Microsoft.Azure.Search; using Microsoft.Azure.Search.Models; using Microsoft.WindowsAzure.Storage.Table; [SerializePrope
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;
}
此操作将运行并成功创建索引。请参见下文
现在,如果我尝试向该表中添加一个实体,我会收到一个错误请求,但是在实体中注释掉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);
}
如果我从实体中删除分区键,并重新运行相同的代码以重新创建索引,则会成功执行相同的代码段。
我注意到的是,我的实体上现在有两个分区键,其中一个将保持为空(参见下图),并且我的属性不会覆盖原始分区键
这里缺少什么吗?根据您的代码,我发现您的资产使用了new关键字来修改基类的分区属性 但这只会隐藏base.partition,而不会覆盖它
public new string PartitionKey { get; set; }
在资产类中设置值后,您会发现它包含两个分区,如下所示:
[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;
}
因此,如果基类的分区键值为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;
}
如果您想使用表存储作为数据源,我建议您可以参考以下内容 您提到您正在使用Azure存储SDK,但代码是针对搜索服务SDK的。你能解释一下吗?对不起,我在想什么,我会纠正的,如果你把它作为一个控制台应用程序运行,你可以跟踪像Fiddler这样的工具的请求/响应。您应该看到有关错误的更多详细信息。谢谢,现在它实际上是有意义的。我也会看看这篇文章,以便更好地理解这一点。