0

I am setting up a new C# project that needs to connect to an Azure CosmosDb instance. We need to have have all properties in the database be camelCase.

Below is the base class to connect to the db and a Shipment class I have setup. I can tell when stepping through the code that it is set to camelCase. However, I get the error

'The requested partition key path '/CompanyId' does not match existing Container 'shipment' with partition key path '/companyId' (Parameter 'PartitionKey')'

when starting the code and ensuring the container exists. Additionally, if I set the CompanyId property name to companyId, everything works, except all other PascalCased C# properties are not switchedd to camelCase when being inserted into the CosmoDb.

Additionally, I am using the nuget package EFCore.NamingConventions to make switching to camelCase easier. What do I need to do or change to get my PascalCased properties to match to my camelCase partionKey and for everything to be stored in camelCase?

using Microsoft.EntityFrameworkCore;

public class CosmoDbContext : DbContext
{
    public CosmoDbContext(DbContextOptions<CosmoDbContext> options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder = optionsBuilder.UseCamelCaseNamingConvention();

        DbContextOptionsBuilder options = optionsBuilder.UseCosmos(
        accountEndpoint: "https://....azure.com:443/",
        accountKey: "...",
        databaseName: "...");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Shipment>().ToContainer("shipment")
                .HasPartitionKey(s => s.CompanyId);
    }

    public DbSet<Shipment> Shipments { get; set; }
}

public class Shipment
{
    public required string Id { get; set; }

    public required CompanyId CompanyId { get; set; }
}

1
  • In Azure Cosmos DB, must and should match the partition key or else you can modify the partition key and then you can access the particular db or a container. You can modify the partition key in an existing container, or you can create a new container with the required partition key. Please refer to this article. Commented Feb 17 at 12:22

1 Answer 1

1

The requested partition key path '/CompanyId' does not match existing Container 'shipment' with partition key path '/companyId' (Parameter 'PartitionKey')

I also tried in my environment and faced the same error as shown above. In Azure Cosmos DB, we can change partition key manually and then it is possible for switching into camelCase. Follow the below steps on how to change partition key in Azure Cosmos DB:

Step 1:

Open Azure Cosmos DB container and click on Settings and go to Partition Keys and select Change. enter image description here

Step 2:

Select either New container or Existing container, i selected New container and created a new container named shipment2 and provided partition key as CompanyId and select OK as shown in the below image. enter image description here

Now, try with the below code it will successfully save the data in camelCase in the Azure Cosmos DB Container as shown in the below output.

class Program
{
    static async Task Main()
    {
        Console.WriteLine("Connecting to Cosmos DB...");

        var optionsBuilder = new DbContextOptionsBuilder<CosmoDbContext>();
        optionsBuilder.UseCosmos(
            "https://<acc_name>.documents.azure.com:443/",
            "<primary_key>",
            "db1") 
        .UseCamelCaseNamingConvention(); 

        using var dbContext = new CosmoDbContext(optionsBuilder.Options);

        await dbContext.Database.EnsureCreatedAsync(); 

        var newShipment = new Shipment
        {
            Id = Guid.NewGuid().ToString(),
            CompanyId = "Company-123",
            ShipmentName = "Test Shipment"
        };

        dbContext.Shipments.Add(newShipment);
        await dbContext.SaveChangesAsync();

        Console.WriteLine($"Shipment '{newShipment.ShipmentName}' added with ID {newShipment.Id}");

        var shipments = await dbContext.Shipments.ToListAsync();
        Console.WriteLine("\nShipments in database:");
        foreach (var shipment in shipments)
        {
            Console.WriteLine($"- {shipment.Id}: {shipment.ShipmentName} (Company ID: {shipment.CompanyId})");
        }
    }
}

public class CosmoDbContext : DbContext
{
    public CosmoDbContext(DbContextOptions<CosmoDbContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Shipment>()
            .ToContainer("shipment2") 
            .HasPartitionKey(s => s.CompanyId); 
    }

    public DbSet<Shipment> Shipments { get; set; }
}

public class Shipment
{
    public required string Id { get; set; }

    [JsonPropertyName("CompanyId")] 
    public required string CompanyId { get; set; }

    [JsonPropertyName("shipmentName")] 
    public required string ShipmentName { get; set; }
}

Output:

Connecting to Cosmos DB...
Shipment 'Test Shipment' added with ID d2d043fc-f03d-4bc2-b16f-de9238e8e97e

Shipments in database:
- d2d043fc-f03d-4bc2-b16f-de9238e8e97e: Test Shipment (Company ID: Company-123)

enter image description here

Note: Refer to this article for more information on Partition Key in Azure Cosmos DB.

Sign up to request clarification or add additional context in comments.

Comments

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.