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: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; }
}