【问题标题】:PartitionKey value must be supplied for this operation in cosmosdb delete operaiton必须在 cosmos db 删除操作中为此操作提供 PartitionKey 值
【发布时间】:2019-10-30 07:43:57
【问题描述】:

我正在尝试从 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”作为第二个参数发送,但它不起作用

我正在使用宇航员

【问题讨论】:

  • RemoveByIdAsync 不是 Cosmos DB SDK 的一部分,您能否分享该方法的代码以及它如何调用 Cosmos DB SDK?
  • Cosmonaut 的一部分,是 cosmosdb 使用最广泛的第三方 sdk

标签: c# asp.net .net azure azure-cosmosdb


【解决方案1】:

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

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

编辑:

Cosmonaut SDK 满足您的需求

您需要提供分区键值而不是分区键 删除时的定义。您的删除请求应如下所示, 假设 id 是您的分区键。

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

【讨论】:

  • 我的实体中没有自我。
  • 为什么是宇航员?您可以直接使用 cosmosdb sdk。但是我更新了我的答案
  • 因为我用cosmonaut 2年多了,比官方SDK好,但是方法调用很相似。
【解决方案2】:

您需要将要删除的元素的分区键的作为第二个参数,而不是路径和属性名称。

var result = await CosmosStoreHolder.Instance.CosmosStorePartner.RemoveByIdAsync(id, "<partition key value for that id>");

由于您定义为 PK 的属性是 CosmosEntityName,因此您需要该文档的该属性的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2022-07-16
    相关资源
    最近更新 更多