C# 使用 Upsert 更新 Azure Cosmos Db json 项目

发布于 2025-01-10 17:02:14 字数 956 浏览 0 评论 0原文

我(CosmosDb 的新手)正在尝试更新 Azure Cosmos 数据库中的项目,但它正在插入。

发送的对象是

public class Bank
{
    public string id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

我尝试仅传递回代码和名称值(即我没有在对象中包含 id,这会导致插入(我想要更新)。

我现在包含会导致错误的 id。

返回的错误是:

ResourceType Document 是意外的。 ActivityId:a0d50426-c556-4b17-9646-93052699026e,Windows/10.0.19044 documentdb-netcore-sdk/2.16.2

因此,前端更新后的值(仅更改名称值)为:

Code: "FNB"
Name: "aa First Update Test"
id:   "d76ade3d-7d02-46e5-a458-e9f0781bf044"

Cosmos db image

DAL 代码:

var documentUri = UriFactory.CreateDocumentUri(DBName, "Banks", bank.Code);
try
{
    Document doc = await client.UpsertDocumentAsync(documentUri, bank);
}

如何更新?

TIA

I'm (new to CosmosDb) and trying to update an item in an Azure Cosmos db, but it's inserting instead.

The object sent is

public class Bank
{
    public string id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

I tried passing only the Code and the Name values back (i.e. I did not include the id in the object, which causes an insert (I wanted an update).

I am now including the id which results in an error.

Error returned is:

ResourceType Document is unexpected.
ActivityId: a0d50426-c556-4b17-9646-93052699026e, Windows/10.0.19044 documentdb-netcore-sdk/2.16.2

So, it's values after a front end update (only changing the Name value) are:

Code: "FNB"
Name: "aa First Update Test"
id:   "d76ade3d-7d02-46e5-a458-e9f0781bf044"

Cosmos db image

The DAL code:

var documentUri = UriFactory.CreateDocumentUri(DBName, "Banks", bank.Code);
try
{
    Document doc = await client.UpsertDocumentAsync(documentUri, bank);
}

How do I get it to update?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2025-01-17 17:02:14

您的代码不清楚并且没有足够的信息。请尝试这些功能。

protected DataContext(string endpointUrl, string databaseId,   
                      string masterKey)  
{  
    _databaseId = databaseId;  
    _masterKey = masterKey;  
  
    _databaseUri = UriFactory.CreateDatabaseUri(_databaseId);  
  
    this._client = new DocumentClient(new Uri(endpointUrl), _masterKey);  
  
    this._client.CreateDatabaseIfNotExistsAsync(new Database   
      { Id = _databaseId });  
  
    this._client.CreateDocumentCollectionIfNotExistsAsync(  
      UriFactory.CreateDatabaseUri(_databaseId),   
      new DocumentCollection { Id = CollectionId });  
  
    _databaseCollectionUri = UriFactory.CreateDocumentCollectionUri(  
      _databaseId, CollectionId);  
}  

插入和更新;

public async Task<Document> UpsertDocumentAsync(T entity)  
{  
    var result = await this._client.UpsertDocumentAsync(  
    _databaseCollectionUri, entity);  
    return result;  
}  

使用或尝试使用 nuget Microsoft.Azure.Cosmos

 string cosmosDbConnectionString = CosmosDbConnectionKey;
                CosmosClient cosmosClient = new CosmosClient(cosmosDbConnectionString);
                var db = CosmosDbNameKey;
                var container = ContainerKey;
                await container.UpsertItemAsync(Model, new PartitionKey(Model.PK));

Your Code is not clear and dont have enough information.try these functions.

protected DataContext(string endpointUrl, string databaseId,   
                      string masterKey)  
{  
    _databaseId = databaseId;  
    _masterKey = masterKey;  
  
    _databaseUri = UriFactory.CreateDatabaseUri(_databaseId);  
  
    this._client = new DocumentClient(new Uri(endpointUrl), _masterKey);  
  
    this._client.CreateDatabaseIfNotExistsAsync(new Database   
      { Id = _databaseId });  
  
    this._client.CreateDocumentCollectionIfNotExistsAsync(  
      UriFactory.CreateDatabaseUri(_databaseId),   
      new DocumentCollection { Id = CollectionId });  
  
    _databaseCollectionUri = UriFactory.CreateDocumentCollectionUri(  
      _databaseId, CollectionId);  
}  

insert and update using

public async Task<Document> UpsertDocumentAsync(T entity)  
{  
    var result = await this._client.UpsertDocumentAsync(  
    _databaseCollectionUri, entity);  
    return result;  
}  

Or Try please using the nuget Microsoft.Azure.Cosmos;

 string cosmosDbConnectionString = CosmosDbConnectionKey;
                CosmosClient cosmosClient = new CosmosClient(cosmosDbConnectionString);
                var db = CosmosDbNameKey;
                var container = ContainerKey;
                await container.UpsertItemAsync(Model, new PartitionKey(Model.PK));
心舞飞扬 2025-01-17 17:02:14

我需要的是Upsert中的DocumentCollection(DocumentCollection Link),但我有Document Link(documentUri)
所以,

    public async Task<ExBool> UpdateAsyncPOCO(Bank bank)
    {
        
        //  NB:  UpsertDocumentAsync should take the DocumentCollection link, instead of Document link.
        // This is a DocumentLink
        var documentUri = UriFactory.CreateDocumentUri(DBName, "Banks", bank.Code);

        // This is a DocumentCollection
        var CollectionUri = UriFactory.CreateDocumentCollectionUri("demo", "Banks");


        try
        {
            Document doc = await client.UpsertDocumentAsync(CollectionUri, bank);
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }

        return result;
    }

插入和更新现在可以完美地工作了。
更新的模型和值:

Code: "updated   FNB 2"
Name: "updated   First National Bank 22"
id: "d76ade3d-7d02-46e5-a458-e9f0781bf044"

类似地,插入

Code: "qwerty"
Name: "qwertyuiop"
id: ""

What I needed was the DocumentCollection (DocumentCollection Link) in the Upsert, but I had the Document Link (documentUri)
So,

    public async Task<ExBool> UpdateAsyncPOCO(Bank bank)
    {
        
        //  NB:  UpsertDocumentAsync should take the DocumentCollection link, instead of Document link.
        // This is a DocumentLink
        var documentUri = UriFactory.CreateDocumentUri(DBName, "Banks", bank.Code);

        // This is a DocumentCollection
        var CollectionUri = UriFactory.CreateDocumentCollectionUri("demo", "Banks");


        try
        {
            Document doc = await client.UpsertDocumentAsync(CollectionUri, bank);
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }

        return result;
    }

Insert and update work perfectly now.
The model and values for the update:

Code: "updated   FNB 2"
Name: "updated   First National Bank 22"
id: "d76ade3d-7d02-46e5-a458-e9f0781bf044"

Similarly, the Insert

Code: "qwerty"
Name: "qwertyuiop"
id: ""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文