添加 CosmosDB 实体,(无法解析租户类型实体的 iD)

Adding a CosmosDB entity, (Unable to resolve iD for entity of type Tenant)

我正在尝试使用以下包添加 cosmosdb 文档:

https://github.com/Elfocrash/Cosmonaut

api控制器是这样的:

  [HttpPut]
        public async Task<IHttpActionResult> PutTenant([ModelBinder(typeof(TenantModelBinder))] Tenant tenant)
        {
            //var provider = new MultipartMemoryStreamProvider();
            //var contentType = "";
            //var content = new byte[0];
            //await base.Request.Content.ReadAsMultipartAsync(provider);
            //if (provider.Contents.Count > 0)
            //{
            //    contentType = provider.Contents[0].Headers.ContentType.MediaType;
            //    content = await provider.Contents[0].ReadAsByteArrayAsync();
            //}


            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureStorageKey"].ToString());
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["certificatesContainer"].ToString());

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

            // Create or overwrite the "myblob" blob with contents from a local file.
            //blockBlob.Properties.ContentType = tenant.ContentType;
            MemoryStream stream = new MemoryStream(tenant.CertificateFile);
            blockBlob.UploadFromStream(stream);

            var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>();
            tenant.CertificatePath = blockBlob.Uri;

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

            var added = await tenantStore.AddAsync(tenant);
            return StatusCode(HttpStatusCode.NoContent); 
        }

但是我得到这个错误:

Unable to resolve iD for entity of type Tenant

我的租客class:

  public class Tenant
    {
        public string TenantId { get; set; }
        public string TenantUrl { get; set; }
        public Uri CertificatePath { get; set; }
        public string CertificatePassword { get; set; }

        public byte[] CertificateFile { get; set; }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

如 Github 自述文件页面所述 Restrictions section

Because of the way the internal id property of Cosmosdb works, there is a mandatory restriction made. You cannot have a property named Id or a property with the attribute [JsonProperty("id")] without it being a string. A cosmos id need to exist somehow on your entity model. For that reason if it isn't part of your entity you can just implement the ICosmosEntity interface or extend the CosmosEntity class.

针对您的情况,建议的修复方法是使用 [JsonProperty("id")] 属性修饰 TenantId