使用C#将文档插入CosmosDB

问题描述

我正在尝试向Cosmos仿真器中使用sqlAPI在本地运行的cosmos数据库中插入新文档。选择文档工作正常,但是当我尝试使用以下代码插入时,出现以下错误

响应状态代码未指示成功:未找到(404);子状态:1003; ActivityId:10e20d81-559a-47b3-8eef-021ce4138279;原因:(消息:{“错误”:[“所有者资源不存在”]}

代码

async Task<Product> IProductRepository.Add(Product product)
        {
            var container = cosmosClient.GetContainer("Invertory","Products");
            product.Id = Guid.NewGuid().ToString();
            product.Category = new Category() { Name = "test" };
            var x = await container.UpsertItemAsync<Product>(product);
            return product;
        }

我尝试达到的资源(数据库)似乎不存在,但是当我使用相同的数据库和容器并使用以下代码进行查询时,就会得到预期的结果。

async Task<Product> IProductRepository.Get(string Id)
        {            
            var container = cosmosClient.GetContainer("Inventory","Products");
            var iterator = (from p in container.GetItemLinqQueryable<Product>()
                            where p.Id.Equals(Id)
                            select p).ToFeedIterator();

            return iterator.HasMoreResults ? (await iterator.ReadNextAsync()).First() : null;
        }

在这里想念什么?

解决方法

函数“ IProductRepository.Add”中有一个错字。 库存应为库存