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

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

我正在尝试从 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/

相关文章:

c# - 如何使用protobuf.net序列化dictionary<int, List<CustomeObject>>?

c# - 如何在 .NET 中将提示/标记写入 WAV 文件

c# - 如何将值从 View 传递到 Controller

c# - 使用带有 ItemRequired = Required.Always 的 Json.Net 反序列化时忽略属性

.net - 使 WinForm 窗体在多个屏幕上最大化到桌面高度

.net - Linq to SQL - 如何将数字作为字符串排序?

c# - 在 Mono 中处理通信的简单跨平台进程?

c# - 如何获取一个类扩展的所有类的 Type[]

asp.net - 为什么选择 MVC 而不是优秀的旧式 ASP.NET?还是不明白为什么我应该走这条路?

asp.net - 在IIS中编辑配置自定义部分