添加一个CosmosDB实体(无法为Tenant类型的实体解析iD) [英] Adding a CosmosDB entity, (Unable to resolve iD for entity of type Tenant)

查看:81
本文介绍了添加一个CosmosDB实体(无法为Tenant类型的实体解析iD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

https://github.com/Elfocrash/Cosmonaut

api控制器是这样的:

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

但是我收到此错误:

Unable to resolve iD for entity of type Tenant

我的房客班级:

  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自述页面在限制部分

As the Github Readme page states in the Restrictions section

由于Cosmosdb的内部id属性的工作方式,因此进行了强制性限制.如果没有字符串,则不能具有名为Id的属性或具有[JsonProperty("id")]属性的属性.宇宙ID需要以某种方式存在于您的实体模型上.因此,如果它不是您实体的一部分,则可以实现ICosmosEntity接口或扩展CosmosEntity类.

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.

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

这篇关于添加一个CosmosDB实体(无法为Tenant类型的实体解析iD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆