最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Using camelCase on CosmoDb DbContextOptionBuilder is not working setting output to camelCase and breaks HasPartitionKey - S

programmeradmin2浏览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: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; }
}

发布评论

评论列表(0)

  1. 暂无评论