MongoDB.EntityFrameworkCore with CamelCaseElementNameConvention

Hi,

I’m new to MongoDB.EntityFrameworkCore and I’m trying to use CamelCaseElementNameConvention in my solution. I’ve registered this convention before instantiating my DbContext. My entity class properties are named using PascalCase.

I was expecting that a class like the following:

public class User
{
public Guid UserId { get; private set; }
public string Username { get; private set; }
}

would be persisted as:

{
_id: UUID(‘5727f6b9-e4d1-458e-85f8-b761c32da765’),
username: “test”
}

However, the PascalCase notation is still being retained, and the document is stored as:

{
_id: UUID(‘5727f6b9-e4d1-458e-85f8-b761c32da765’),
Username: “test”
}

It seems that CamelCaseElementNameConvention is being ignored. Is this expected behavior? Does it only work with the native MongoDB driver’s serializer, or is it also supported by Entity Framework Core?

I’ve resorted to using the Fluent API method HasElementName, which works, but it would be great if this could be configured globally.

Also, I would prefer to avoid using attribute based decorators, as I’d like to keep my domain entities persistence agnostic.

Thanks in advance for your help.

Best regards,
David

Hi David.

As far as I can see this should be working and we have tests for this behavior.

Can you supply a minimal repro that shows this going wrong so we can investigate?

This was driving me crazy. After doing some digging through the MongoDB.EntityFrameworkCore repo I found that the ef providor has its own conventions in MongoDB.EntityFrameworkCore.Metadata.Conventions. You add a convention with the method override on your dbcontext

public class MongoDbContext : DbContext
{
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder.Conventions.Add(_ => new CamelCaseElementNameConvention());
    }
}