简体   繁体   English

使用 C# 将文档插入 CosmosDB

[英]Inserting document into CosmosDB with C#

I'm trying to insert a new document to a cosmos database running locally with SQLAPI in the Cosmos Emulator.我正在尝试将新文档插入到在 Cosmos 模拟器中使用 SQLAPI 在本地运行的 cosmos 数据库。 Selecting documents works fine but when I try to insert with below code I get the following error:选择文档工作正常,但是当我尝试使用以下代码插入时,出现以下错误:

Response status code does not indicate success: NotFound (404);响应状态码不表示成功:NotFound(404); Substatus: 1003;子状态:1003; ActivityId: 10e20d81-559a-47b3-8eef-021ce4138279; ActivityId:10e20d81-559a-47b3-8eef-021ce4138279; Reason: (Message: {"Errors":["Owner resource does not exist"]}原因:(消息:{“错误”:[“所有者资源不存在”]}

Code:代码:

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

It seems that the recourse (database) I try to reach not exists, but when I do a query with the same database and container and with the following code I get the expected results.似乎我尝试访问的资源(数据库)不存在,但是当我使用相同的数据库和容器并使用以下代码进行查询时,我得到了预期的结果。

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

What am I missing here?我在这里错过了什么?

There is a typo in function "IProductRepository.Add". function“IProductRepository.Add”中有错字。 Invertory should be Inventory .库存应该是库存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM