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

Please support Json nested name convertions. #269

Open
ye4241 opened this issue Feb 18, 2024 · 3 comments
Open

Please support Json nested name convertions. #269

ye4241 opened this issue Feb 18, 2024 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@ye4241
Copy link

ye4241 commented Feb 18, 2024

I noticed that there is a TODO in code:

// TODO: Note that we do not rewrite names of JSON properties (which aren't relational columns).
// TODO: We could introduce an option for doing so, though that's probably not usually what people want when doing JSON

Could you please add feature for this?

                    foreach (var property in entityType.GetProperties())
                    {
                        property.Builder.HasNoAnnotation(RelationalAnnotationNames.ColumnName);
                        property.Builder.HasJsonPropertyName(_namingNameRewriter.RewriteName(property.GetColumnName()));
                    }
@roji roji added this to the 9.0.0 milestone Feb 18, 2024
@roji roji added the enhancement New feature or request label Feb 18, 2024
@roji roji modified the milestones: 9.0.0, Backlog Feb 18, 2024
@roji
Copy link
Member

roji commented Feb 18, 2024

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.

@Rlamotte
Copy link

Rlamotte commented Mar 22, 2024

@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 :

  1. Create a stringExtension to be able to convert to your defined convention (in my case, camelCase) :
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..]}";
    }
}
  1. Add a specific modelBuilder extension to apply this convention to json owned properties :
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;
   }
}
  1. Call this function in your dbContext file :
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();
   }
}

@ye4241
Copy link
Author

ye4241 commented Mar 22, 2024

@Rlamotte Indeed, the underlying logic is to use JsonPropertyName to batch set the properties at each nest elements. The current project already has comprehensive CamelCase-related methods, so the ideal way is to complete these batch settings within the existing project. I will try to see if I can support batch setting the JsonPropertyName attribute at the lower level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants