Trying to do insert to CosmosDb using DaprClient. Below is code
[HttpPost("saveData")]
public async Task<ActionResult<string>> SaveData([FromBody] InputMessage message, [FromServices] DaprClient daprClient)
{
Dictionary<string, string> data = new Dictionary<string, string> { { "partitionKey", "ServiceName" } };
if (message is null)
return new BadRequestObjectResult(new { error = $"The client is null" });
var state = await daprClient.GetStateEntryAsync<TestModel>(_cosmosStoreName, "ServiceName", metadata: data);
if (state.Value != null)
{
return new BadRequestObjectResult(new { error = $"The client with id {message.id} already exists" });
};
state.Value = new TestModel
{
ServiceName = "ServiceName"
};
await state.SaveAsync();
return "Good";
}
PartitionKey is ServiceName. When I run dapr cli, and make request, I get below error
{\"code\":\"BadRequest\",\"message\":\"Message: {\\\"Errors\\\":[\\\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more: https:\\\\/\\\\/aka.ms\\\\/CosmosDB\\\\/sql\\\\/errors\\\\/wrong-pk-value\\\"]}\\r\\nActivityId: 6ca949f9-da2a-4fe9-a346-a67196817670
When i trace for log with above activityid in CosmosDb, i see in AzureDiagnostics, for Category PartitionKeyRUConsumption, partitionKey_s as "fanglava-coyote||ServiceName"
So my guess is DAPR is giving out partitionKey like that. What am i doing wrong here? Can you please guide how it need to be implemented?
metadata parameter was added later on, when call by just passing key gave same result.
idis not the same thing aspartitionKey(unless you literally choose/idas your partition key). And you should be specifying a value of a document's partition key when writing, not the field name. As foridvalue: Seems like Dapr is generating an id for you (which every document needs). Please be sure to edit your question with additional detail - it shouldn't be in comments.