-
Notifications
You must be signed in to change notification settings - Fork 77
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 apply naming convention to json properties? #65
Comments
@Arsync properties inside JSON documents aren't covered by this plugin, at least not yet. The way JSON support is currently implemented, EF Core is completely unaware of everything happening inside a JSON document (it isn't part of the EF model), so this plugin can't rewrite any names. The way to manage this would be via the JsonSerializerOptions of System.Text.Json, which is used to actually serialize/deserialize JSON documents. Setting in the PG EF provider JsonSerializerOptions isn't currently supported out of the box either (npgsql/efcore.pg#1107), but see this workaround which should provide a solution. Am going to close this for now, but will reopen if JSON support changes in a way that makes it relevant. |
Tried to override mapping for NpgsqlDbType.Jsonb with JsonSerializerOptions naming policy changed to SnakeCaseNamingPolicy, but Following code not worked: private static void OverrideJsonType(NpgsqlDbType type)
{
var origJsonbMapping =
NpgsqlConnection.GlobalTypeMapper.Mappings.Single(m => m.NpgsqlDbType == type);
NpgsqlConnection.GlobalTypeMapper.RemoveMapping(origJsonbMapping.PgTypeName);
NpgsqlConnection.GlobalTypeMapper.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = origJsonbMapping.PgTypeName,
NpgsqlDbType = origJsonbMapping.NpgsqlDbType,
DbTypes = origJsonbMapping.DbTypes,
ClrTypes = origJsonbMapping.ClrTypes,
InferredDbType = origJsonbMapping.InferredDbType,
TypeHandlerFactory = new JsonbHandlerFactory(new JsonSerializerOptions
{
PropertyNamingPolicy = new SnakeCaseNamingPolicy()
})
}.Build());
} Used as: b.UseNpgsql(GetDefaultConnectionString(), opts =>
{
opts.UseNodaTime();
OverrideJsonType(NpgsqlDbType.Json);
OverrideJsonType(NpgsqlDbType.Jsonb);
OverrideJsonType(NpgsqlDbType.JsonPath);
})
.UseSnakeCaseNamingConvention(); Still got queries like SELECT e.settings->>'DefaultArea' AS "DefaultArea"
... |
You're right - I didn't mention that the JsonSerializerOptions apply when System.Text.Json is loading or saving the JSON document to the database, but not when constructing queries that drill into it. For this scenario, the only option you currently have is to specify |
I have models like
Query for data:
And with
.UseSnakeCaseNamingConvention()
it still looks like this in SQL:but needed:
I've tried even this (without success):
The text was updated successfully, but these errors were encountered: