Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify SerializerOptions when working with UseCosmos ? #28270

Closed
benrhere opened this issue Jun 19, 2022 · 3 comments
Closed

How to specify SerializerOptions when working with UseCosmos ? #28270

benrhere opened this issue Jun 19, 2022 · 3 comments

Comments

@benrhere
Copy link

benrhere commented Jun 19, 2022

Ask a question

When instantiating a CosmosClient directly, I can do something like:

new CosmosClient(
                configuration["CosmosDb:Uri"],
                configuration["CosmosDb:ReadWriteKey"],
                new CosmosClientOptions()
                {
                    SerializerOptions = new CosmosSerializationOptions()
                    {
                        PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase,
                    }
                })
            );

which allows me to specify the desired SerializerOptions (specifically the PropertyNamingPolicy).

But when I'm leveraging EF Core with UseCosmosDb similar to:

services.AddDbContext<CosmosDbContext>(options =>
            {
                options.UseCosmos(
        "https://localhost:8081",
        "exampleKey",
        databaseName: "exampleDb");
            });

, how could I achieve the samer result where I specify "PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase" for the CosmosDB connection which the EF Core context will use?

Include provider and version information

EF Core version: 6.0.6
Database provider: (e.g. Microsoft.EntityFrameworkCore.Cosmos)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.2.4)

@ajcvickers
Copy link
Member

@benrhere This is covered by #17306.

@AndriySvyryd
Copy link
Member

EF actually doesn't use the names provided by the serializer. You can specify the desired name during model building:
b.Property(p => p.Name).ToJsonProperty("name")

And similarly for embedded entities:

b.OwnsOne(
    o => o.ShippingAddress,
    sa =>
    {
        sa.ToJsonProperty("address");
        sa.Property(p => p.Street).ToJsonProperty("shipsToStreet");
        sa.Property(p => p.City).ToJsonProperty("shipsToCity");
    });

You can also use the Metadata property to iterate over all the properties in the model.

@hgchethankumar
Copy link

EF actually doesn't use the names provided by the serializer. You can specify the desired name during model building: b.Property(p => p.Name).ToJsonProperty("name")

And similarly for embedded entities:

b.OwnsOne(
    o => o.ShippingAddress,
    sa =>
    {
        sa.ToJsonProperty("address");
        sa.Property(p => p.Street).ToJsonProperty("shipsToStreet");
        sa.Property(p => p.City).ToJsonProperty("shipsToCity");
    });

You can also use the Metadata property to iterate over all the properties in the model.

How to mention IgnoreNullValues = true in UseCosmos options?

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants