Skip to content

Code templates for scaffolding entity types and DbContext from an existing database #4038

Description

@rowanmiller

We have one seam at the moment where folks can get a hold of the IModel we build from the database and manipulate it. We haven't really done any usability testing on this to see if it is good for changing table name conventions, etc..

We will also want some higher level extensibility points, such as code generation templates.

Activity

  1. added this to the Backlog milestone on Dec 10, 2015
  2. abelepereira commented on Dec 18, 2015

    @abelepereira

    Where is this IModel manipulation being allowed? I guess some sort of extension over the default ef command (EntityFramework.Commands) need to be implemented, but where? Also: does this "extension" for model customization implies creating a new command assembly or will the default EntityFramework.Commands allow some sort of DI?

    Regards

  3. rowanmiller commented on Dec 22, 2015

    @rowanmiller
    ContributorAuthor

    The seam we have at the moment is designed for providers to use... so yes, you probably need to implement at least some parts of the provider to use it. This issue is about creating some high level seams that can be used by the application developer.

  4. dazinator commented on Dec 24, 2015

    @dazinator

    Just wanted to check my understanding - as of the present release there is no way for an application developer to customise the code generated via scaffolding (a provider writer can), although in earlier releases there was a razor based template approach which has since been pulled? So essentially to do stuff like pluralisation etc we are waiting for the resolution of this issue - is that correct?

  5. rowanmiller commented on Dec 28, 2015

    @rowanmiller
    ContributorAuthor

    @dazinator yes, that is correct. Post the 7.0.0 release we plan to provide customizable code-gen templates and code based interception points where you can customize the reverse engineered model and how it is written out to code.

  6. idg10 commented on Dec 30, 2015

    @idg10

    One problem with this approach is that the existing model seems to support just one Name for an entity, which is used for both the generated type, and also for the property on the context. This means that if you want an entity type called Customer but and you want the DbSet<Customer> on your context to be called Customers, it doesn't look like you can do it, because the code gen pulls the name of both the type and that property from the IEntitySet.Name property.

    I've worked through an attempt to singularize some names. (I'm working with a database where all the tables have plural names. Leaving this as is produces correct-sounding dbset properties, but wrong-sounding entity types. E.g., new Customers is a weird way to create a single customer. Singular names produce wrong-sounding dbset props, but I find that slightly less disturbing.)

    Having done this, I'm not sure the approach of modifying the model feels right, because the name you want depends on where the name is actually being used - a property referring to a collection of entities of some type typically wants a different name than one referring to a single property, and some people might want yet another convention again for the mapped table name. I'd prefer the ability to supply an interface (e.g., INameMapper) with methods such as GetSingularNavigationPropertyName, GetMultipleNavigationPropertyName, GetEntityTypeName, and maybe even GetContextDbSetPropertyName.

    There's already a partial implementation of this concept in RelationalScaffoldingModelFactory, which has GetEntityTypeName and GetPropertyName methods you can override. My ideal solution to this would have this fleshed out so I can control everything, and for it to be separated out, so that I'm not obliged to derive from, say, SqlServerScaffoldingModelFactory just to be able to plug in my logic. (Pluralizing/singularizing of identifiers used in code doesn't feel like it should have a dependency on a database-specific type, but as far as I can tell, I needed to make my own derived SqlServerScaffoldingModelFactory to be able to use this extensibility point.)

  7. jjwilliams42 commented on Sep 30, 2017

    @jjwilliams42

    I realize this is going on 2 years old, but was anything changed here that might make customization easier?

  8. tonysneed commented on Oct 5, 2017

    @tonysneed

    👍 For providing customizable code gen templates (for ex, T4 or handlebars), but doing so in a way that can feed into dotnet ef scaffold, so that it's cross platform and not tied to Visual Studio.

  9. bricelam commented on Oct 9, 2017

    @bricelam
    Contributor

    No progress yet. Our main focus for Reverse Engineering is enable updating the model to incorporate changes made to the database after the initial scaffold. #831 During update, we'll try to preserve any manual changes that have been made to the model (e.g. renames and inheritance) which mitigates some of the reasons you might be asking for this feature. But there are still plenty of other reasons to do this feature. It's just a matter of prioritization.

  10. tonysneed commented on Oct 10, 2017

    @tonysneed

    In #9676 @ajcvickers states you would accept a PR to add virtual methods for extensibility. If that's the case, I'd be happy to submit a PR for this.

  11. ajcvickers commented on Oct 10, 2017

    @ajcvickers
    Contributor

    @tonysneed Yep. There was a PR (#9908) but it was abandoned. We would want protected virtual methods.

  12. bricelam commented on Oct 10, 2017

    @bricelam
    Contributor

    If anyone is interested in poking around the internal components...

    ⚠️Warning: These will break in a future release.

    class MyDesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection services)
            => services
                .AddSingleton<ICSharpDbContextGenerator, MyDbContextGenerator>()
                .AddSingleton<ICSharpEntityTypeGenerator, MyEntityTypeGenerator>();
    }
    
    class MyDbContextGenerator : CSharpDbContextGenerator
    {
        public MyDbContextGenerator(
            IScaffoldingProviderCodeGenerator providerCodeGenerator,
            IAnnotationCodeGenerator annotationCodeGenerator,
            ICSharpUtilities cSharpUtilities)
            : base(providerCodeGenerator, annotationCodeGenerator, cSharpUtilities)
        {
        }
        
        // TODO: Override DbContext generation
    }
    
    class MyEntityTypeGenerator : CSharpEntityTypeGenerator
    {
        public MyEntityTypeGenerator(ICSharpUtilities cSharpUtilities)
            : base(cSharpUtilities)
        {
        }
        
        // TODO: Override entity type generation
    }
  13. tonysneed commented on Oct 10, 2017

    @tonysneed

    I can pick this one up. Already signed a contrib agreement for ASPNET Core.

  14. tonysneed commented on Oct 11, 2017

    @tonysneed

    For some reason adding the user profile dotnet folder to my path system env variable doesn't seem to work.

    env-var-local-dotnet

    dotnet --version doesn't see it.

    dotnet-version

    However, if I replace the path entirely, it's OK.

    dotnet-version-ok

    Am I doing something obviously wrong? TIA!

    P.S. I can just open the VS solution from the command line, so I can get past the issue. Just wondering about setting the global path and what I might be doing wrong there.

  15. 76 remaining items

  16. added a commit that references this issue on Aug 11, 2022
    e9cdfb7
  17. modified the milestones: 7.0.0, on Aug 11, 2022
  18. ghost closed this as completedin #28678on Aug 11, 2022
  19. ghost added a commit that references this issue on Aug 11, 2022
    7a9756c
  20. modified the milestones: , 7.0.0 on Nov 5, 2022
  21. added theissue type on Jun 15, 2025
  22. marked DbScaffold with C# 9 records #23236 as a duplicate of this issue on Jun 16, 2025
  23. marked Custom Code Generation #21409 as a duplicate of this issue on Jun 16, 2025
  24. marked Customizable scaffolding #20593 as a duplicate of this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions