Luis Valencia
Luis Valencia

Reputation: 33998

PartitionKey value must be supplied for this operation in cosmosdb delete operaiton

I am trying to delete a document from Cosmos DB

My code is like this:

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
    }

But I got this error: PartitionKey value must be supplied for this operation

I was trying to send as string "/CosmosEntityName" as second parameter, but it doesnt work

I am using Cosmonaut

Upvotes: 2

Views: 1975

Answers (2)

Matias Quaranta
Matias Quaranta

Reputation: 15603

You need to pass the value of the partition key of the element you want to delete as second parameter, not the path and attribute name.

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

Since the attribute you have defined as PK is CosmosEntityName, you need that attribute's value for that document.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

You need to use the request options. For example, if your collection is partitioned by CosmosEntityName;

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

EDIT:

Here's what you need with 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) });

Upvotes: 2

Related Questions