gpt4 book ai didi

c# - 必须在 cosmosdb 删除操作中为此操作提供 PartitionKey 值

转载 作者:行者123 更新时间:2023-12-02 23:27:24 25 4
gpt4 key购买 nike

我正在尝试从 Cosmos DB 中删除文档

我的代码是这样的:

public async Task<IHttpActionResult> DeletePartner(string id)
{
var telemetry = new TelemetryClient();
try
{

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}


var customers = await CosmosStoreHolder.Instance.CosmosStoreCustomer.Query().Where(x=> x.PartnerId == id).ToListAsync();
var userStore = CosmosStoreHolder.Instance.CosmosStoreUser;
var users = await userStore.Query().Where(x => x.PartnerId == id).ToListAsync(); ;

if (customers.Count> 0 || users.Count>0)
{
return BadRequest("You cant delete partners with existing customers or users");
}
else
{
var result = await CosmosStoreHolder.Instance.CosmosStorePartner.RemoveByIdAsync(id, "/CosmosEntityName");
return Ok(result);
}
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};

telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
}



[SharedCosmosCollection("shared")]
public class Partner : ISharedCosmosEntity
{
/// <summary>
/// Partner id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }

/// <summary>
/// Partner name
/// </summary>
public string PartnerName { get; set; }

/// <summary>
/// Partner contact name
/// </summary>
public string PartnerContact { get; set; }

/// <summary>
/// Partner contact phone
/// </summary>
public string PartnerPhone { get; set; }

/// <summary>
/// Partner contact Office 365 domain
/// </summary>
public string PartnerDomain { get; set; }

/// <summary>
/// Partner type, silver, gold or platinum
/// </summary>
[ValidEnumValue]
public PartnerType PartnerType { get; set; }

/// <summary>
/// Partner start date
/// </summary>
public DateTime StartDate { get; set; }

/// <summary>
/// Partner end date
/// </summary>
public DateTime EndDate { get; set; }

/// <summary>
/// Parter enabled
/// </summary>
public bool Enabled { get; set; }

/// <summary>
/// CosmosEntityname
/// </summary>
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
}

/// <summary>
/// Partner type Enum
/// </summary>
public enum PartnerType
{
///Silver
Silver,
///Gold
Gold,
///Platinum
Platinum
}

但是我收到了这个错误:必须为此操作提供 PartitionKey 值

我试图将字符串“/CosmosEntityName”作为第二个参数发送,但它不起作用

我正在使用宇航员

最佳答案

您需要使用请求选项。例如,如果您的集合按 CosmosEntityName 分区;

await this.documentClient.DeleteDocumentAsync(productDocument._self, new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey(productDocument.CosmosEntityName) });

编辑:

这就是您需要的Cosmonaut SDK

You need to provide the partition key value not the partition key definition when you delete. Your delete request should look like this, assuming the id is your partition key.

var deleted = await this._cosmonautClient.DeleteDocumentAsync(this._databaseName, collectionName, message.Id, new RequestOptions { PartitionKey = new PartitionKey(message.Id) });

关于c# - 必须在 cosmosdb 删除操作中为此操作提供 PartitionKey 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58620602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com