使用 C# 将文档插入 CosmosDB
Inserting document into CosmosDB with C#
我正在尝试使用 Cosmos 模拟器中的 SQLAPI 在本地将新文档插入到 cosmos 数据库 运行。选择文档工作正常,但当我尝试使用以下代码插入时,出现以下错误:
响应状态码不表示成功:NotFound(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”中有错字。 库存 应该是库存。
我正在尝试使用 Cosmos 模拟器中的 SQLAPI 在本地将新文档插入到 cosmos 数据库 运行。选择文档工作正常,但当我尝试使用以下代码插入时,出现以下错误:
响应状态码不表示成功:NotFound(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”中有错字。 库存 应该是库存。