Adding a CosmosDB entity, (Unable to resolve iD fo

2019-07-28 19:02发布

I am trying to add a cosmosdb document using the following package:

https://github.com/Elfocrash/Cosmonaut

The api controller is this:

  [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); 
        }

However I get this error:

Unable to resolve iD for entity of type Tenant

My 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);
        }
    }

1条回答
来,给爷笑一个
2楼-- · 2019-07-28 19:32

As the Github Readme page states in the 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.

The recommended fix in your case would be to decorate the TenantId with the [JsonProperty("id")] attribute.

查看更多
登录 后发表回答