必须在cosmosdb delete操作中为此操作提供PartitionKey值

PartitionKey value must be supplied for this operation in cosmosdb delete operaiton(必须在cosmosdb delete操作中为此操作提供PartitionKey值)

本文介绍了必须在cosmosdb delete操作中为此操作提供PartitionKey值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用Cosmonaut

推荐答案

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

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

因为您定义为主键的属性是CosmosEntityName,所以您需要该文档的该属性值。

这篇关于必须在cosmosdb delete操作中为此操作提供PartitionKey值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:必须在cosmosdb delete操作中为此操作提供PartitionKey值

基础教程推荐