TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

EnableSchemaFolders should apply different namespace #119

Closed audacity76 closed 4 years ago

audacity76 commented 4 years ago

Hi,

putting the entities into folders is great but the namespace should follow. If so, you can have the same table (name) in different schemas.

By the way the Docs example notes EnableSchemaFolders two times:

            // Put Models into folders by DB Schema
            //options.EnableSchemaFolders = true; <------------------------------------------

            // Exclude some tables
            options.ExcludedTables = new List<string> { "Territory", "EmployeeTerritories" };

            // Add custom template data
            options.TemplateData = new Dictionary<string, object>
            {
                { "models-namespace", "ScaffoldingSample.Models" },
                { "base-class", "EntityBase" }
            };

            // Place models in folders by schema
            //options.EnableSchemaFolders = true; <---------------------------------------------
tonysneed commented 4 years ago

Ah yes, good catch. If you like, you can submit a fix with a PR, or I can also do it.

audacity76 commented 4 years ago

I'll try to provide a PR...

tonysneed commented 4 years ago

Thank you! Be sure to carefully follow the Contributing Guidelines: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/blob/master/.github/CONTRIBUTING.md

audacity76 commented 4 years ago

Alright, I'll try my best to follow the guidelines!

What happens to the names of the DbSets if there is the same table (name) in different schemas? I can change the DbSet's type and prepend the namespace so no problem there but what about the DbSet's name?

public virtual DbSet<SomeSchema.Item> Items { get; set; }
public virtual DbSet<OtherSchema.Item> Items { get; set; } 

Maybe the EntityNameTransformer could provide different names then but currently it cannot recognize the entities schema...

JohnGoldInc commented 4 years ago

@audacity76 Yeah , totally the stuff my mind didn't want to let me consider when I made the PR for EnableSchemaFolders ; plus it will get annoying in the using section too...

Best to put "schema" as a variable sent to the template for;

Then alter the templates to have schema added conditionally (in the appropriate places/uses) on EnableSchemaFolders being true

such that the Set names become: public virtual DbSet<Schema.Item> Schema_Items { get; set; }

Alternately it could be changed in the class name too and be : public virtual DbSet<Schema_Item> Schema_Items { get; set; } (but then it wouldn't need another namespace)

I say put it in the template, because I personally don't want the above, but I can understand others would want it.

tonysneed commented 4 years ago

I would agree to supply schema name in the template data. But don’t alter the hbs templates. Instead, if EnableSchemaFolders is true, prefix then model class name with the schema namespace.

JohnGoldInc commented 4 years ago

@tonysneed So no namespace change? Otherwise would still need to alter hbs for "using" statements in "Imports" of entities and context...

tonysneed commented 4 years ago

Since the namespace for classes in a folder is nested within the parent namespace, if you just prefix the model class name with the folder name, it should all just work: public virtual DbSet<Schema.Item>.

audacity76 commented 4 years ago

Ok, I'll prefix the model type with the schema name and supply the schema in the templates. So everybody can choose there own DbSet name. So for now the names stay the same which is okay if there are no duplicate table names.

Could take a while for the PR as I'm not at work this week.