-1

I'm migrating from Microsoft.Azure.Cosmos.Table to Azure.Data.Tables following this

with cosmos i had this method to insert/merge and return the inserted record

private async Task<OfficeSupplyEntity?> InsertOrMergeAsync(CloudTable table, OfficeSupplyEntity record)
{
    var insertOrMergeOperation = TableOperation.InsertOrMerge(record);
    var result = await table.ExecuteAsync(insertOrMergeOp);
    var insertedCustomer = result?.Result as OfficeSupplyEntity;
    return insertedCustomer;
}

now I want to convert this to use Azure.Data.Tables. But i'm not sure how to return the inserted record. here newRecord is seems to getting the type Azure.Response. How do I extract the inserted record.

private async Task<OfficeSupplyEntity?> InsertOrMergeAsync(TableClient table, OfficeSupplyEntity record)
{
    var insertedCustomer = await table.UpsertEntityAsync(record);
    //how to return inserted?
}

Help appreciated thanks

1 Answer 1

1

How do I extract the inserted record.

You can use the below approach to retrieve the inserted record from Azure Cosmos DB Table API. It uses GetEntityAsync to retrieve the inserted data from the table as shown in the below output.

private static async Task Main(string[] args)
{
    string connectionString = "*****";
    string tableName = "ftab";

    var tableClient = new TableClient(connectionString, tableName);

    var entity = new TableEntity("partitionKey", "rowKey")
    {
        { "Name", "Pavan" },
        { "Age", "26" }
    };

    await tableClient.UpsertEntityAsync(entity);

    TableEntity retrievedEntity = await tableClient.GetEntityAsync<TableEntity>("partitionKey", "rowKey");

    Console.WriteLine($"PartitionKey: {retrievedEntity.PartitionKey}");
    Console.WriteLine($"RowKey: {retrievedEntity.RowKey}");
    foreach (var property in retrievedEntity)
    {
        Console.WriteLine($"{property.Key}: {property.Value}");
    }
}

Output:

PartitionKey: partitionKey
RowKey: rowKey
odata.etag: W/"datetime'2024-08-23T12%3A06%3A26.9334605Z'"
PartitionKey: partitionKey
RowKey: rowKey
Name: Pavan
Age: 26
Timestamp: 8/23/2024 12:06:26 PM +00:00
Sign up to request clarification or add additional context in comments.

1 Comment

i see so you need 2 api calls for insert and read. i thought there was somehow a way of extracting it from the Response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.