TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
210 stars 53 forks source link

How DbSet names can be customized? #209

Open IvanFarkas opened 2 years ago

IvanFarkas commented 2 years ago

How DbSet names can be customized?

I find this tool amazing and invaluable. Thank you very much Tony for your contribution.

I've seen the previous similar questions, but the answer is wage or perhaps I missed something.

I customized Entities massively using the EntityTypeGenerator and ScaffoldingDesignTimeServices. I even added a data dictionary to translate meaningless and totally unnecessary abbreviations (num -> Number, amt -> Amount, ind -> Indicator, etc.) in table names and columns to a modern naming convention that could be a good base for domain driven design.

services.AddHandlebarsTransformers(
  e => Util.UpdateName("Table", e),
  e => Util.UpdateName("Table", e),
  e => new EntityPropertyInfo(Util.UpdateName("Column", e.PropertyType), Util.UpdateName("Column", e.PropertyName), e.PropertyIsNullable),
  e => new EntityPropertyInfo(Util.UpdateName("Column", e.PropertyType), Util.UpdateName("Column", e.PropertyName), e.PropertyIsNullable),
  e => new EntityPropertyInfo(Util.UpdateName("Column", e.PropertyType), Util.UpdateName("Column", e.PropertyName), e.PropertyIsNullable));

I tried to use DbContextGenerator like EntityTypeGenerator, but I could not accomplish it easily. The only alternative I found is to replicate the HbsCSharpDbContextGenerator class and use my version of the GenerateClass() method to set the set-property-name dbSets Dictionary item used in DbSets.hbs

Any help, pointer is greatly appreciated. Again, thx Tony for this marvelous tool!

tonysneed commented 2 years ago

Hi @IvanFarkas,

I think you're on the right track. You'll want to follow guidelines for Taking Full Control by Extending Handlebars Generators. Create a class that inherits from HbsCSharpDbContextGenerator, then override the GenerateClass method. You can see an example of this pattern with the HbsCSharpEntityTypeGenerator in the ef-core-community-handlebars repo.

Thanks also for the kudos!