Search code examples
c#azure-cosmosdb

Response status code does not indicate success: BadRequest (400); Substatus: 0 for basic CosmosDb insert


My object model:

public class PhoneBook
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

my Container creation:

db.CreateContainerIfNotExistsAsync("phoneBooks", "/id")

my insert command:

PhoneBook pb = new() { Id = Guid.NewGuid().ToString(), Name = "hello world };

db.GetContainer("phoneBooks").CreateItemAsync(pb, new PartitionKey(pb.Id));

any idea where I'm going wrong?

I also tried using "/Id" as partition key because I figured it must be case sensitive. However, no joy. Using the data explorer, I can add content just fine, even if I name the property "id" and the partition key is "/Id". This is an item I manually added

{ "id": "4faebd8b-717a-4692-ae00-74aa209d706f", "name": "hello world" }


Solution

  • Response status code does not indicate success: BadRequest (400); Substatus: 0

    Thanks for your suggestion, David. It works well and data got inserted successfully into the Azure Cosmos DB Container by mentioning [JsonProperty("id")] at the Id property, as you can see in the below code.

    public class PhoneBook
    {
        [JsonProperty("id")]
        public string Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
    
    public CosmosDbService(string endpointUri, string primaryKey, string databaseId, string containerId)
    {
        CosmosClientOptions clientOptions = new CosmosClientOptions
        {
            ConnectionMode = ConnectionMode.Direct,
            MaxRetryAttemptsOnRateLimitedRequests = 5,
            MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(30)
        };
    
        _cosmosClient = new CosmosClient(endpointUri, primaryKey, clientOptions);
    
        _database = _cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId).GetAwaiter().GetResult().Database;
    
        _container = _database.CreateContainerIfNotExistsAsync(containerId, "/id").GetAwaiter().GetResult().Container;
    }
    
    public static async Task Main(string[] args)
    {
        string endpointUri = "*****";
        string primaryKey = "*****";
        string databaseId = "db1";
        string containerId = "phoneBooks";
    
        CosmosDbService cosmosDbService = new CosmosDbService(endpointUri, primaryKey, databaseId, containerId);
    
        PhoneBook pb = new PhoneBook
        {
            Id = Guid.NewGuid().ToString(),
            Name = "hello world",
            Description = "Sample description"
        };
    
        await cosmosDbService.CreatePhoneBookItemAsync(pb);
    }
    

    Output:

    {
        "id": "69daea1c-fd73-4c27-89b0-50428f27ef89",
        "Name": "hello world",
        "Description": "Sample description"
    }
    

    enter image description here

    • Go through this link for more information on the error.