-
Notifications
You must be signed in to change notification settings - Fork 75
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
Please support Json nested name convertions. #269
Comments
As the comment says, I doubt this is something that a lot of people actually want - the naming conventions inside JSON documents is usually unrelated to the naming conventions of database columns. I'll put this in the backlog for now to gather more feedback/votes. |
@ye4241 for your information, I encountered exactly the same problem and searched few hours how to solve it. It's quite sad the plugin EFCore.NamingConventions doesn't take it (at least with a parameter) but you can find my solution below :
public static class StringExtension
{
public static string ToCamelCase(this string? text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
//If text is in snake_case, convert each word inside to camelCase
if (text.Contains('_'))
{
var newText = "";
foreach (var word in text.Split('_'))
{
newText += $"{word.ToCamelCase()}_";
}
return newText[..^1];
}
return $"{text.First().ToString().ToLowerInvariant()}{text[1..]}";
}
}
public static class ModelBuilderExtension
{
public static ModelBuilder ConfigureJsonOwnedPropertiesInCamelCase(this ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes()
.Where(entityType => entityType.IsOwned()))
{
foreach (var property in entityType.GetProperties())
{
if (!property.IsPrimaryKey())
{
property.SetJsonPropertyName(property.GetColumnName().ToCamelCase());
}
}
}
return modelBuilder;
}
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().OwnsOne(
order => order.shippingAddress, ownedNavigationBuilder =>
{
ownedNavigationBuilder.ToJson();
ownedNavigationBuilder.OwnsOne(shippingAddress => shippingAddress.Country);
ownedNavigationBuilder.OwnsOne(shippingAddress => shippingAddress.Company);
});
modelBuilder.ConfigureJsonOwnedPropertiesInCamelCase();
}
} |
@Rlamotte Indeed, the underlying logic is to use |
I noticed that there is a TODO in code:
EFCore.NamingConventions/EFCore.NamingConventions/Internal/NameRewritingConvention.cs
Lines 184 to 185 in 83a3412
Could you please add feature for this?
The text was updated successfully, but these errors were encountered: