Entity Framework Core Customize Scaffolding

前端 未结 5 1668
天命终不由人
天命终不由人 2020-12-10 14:19

I have done scaffolding of my SQLServer database. And it creates the POCO objects in the specified folder. What i would like to do is that it extends from my base class. I a

5条回答
  •  情深已故
    2020-12-10 14:26

    In case you want to modify entity names (and file and class names) here's something that might help:

    Based on Chris Peacock's answer (and comments) you can build two classes to modify the names of entities and files (this works in Core 2.2).

    public class CustomEFUtilities : CSharpUtilities
    {
        public override string Uniquifier(
            string proposedIdentifier, ICollection existingIdentifiers)
        {
            var finalIdentifier = base.Uniquifier(proposedIdentifier, existingIdentifiers);
            
            // your changes here
            if (finalIdentifier.StartsWith("tl"))
            {
                finalIdentifier = finalIdentifier.Substring(2);
            }
            
            return finalIdentifier;
        }
    }
    

    And similarly:

    public class CustomEFDesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddSingleton();
        }
    }
    

    Edit (EF Core 3.1)

    A breaking change was introduced (https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#microsoftentityframeworkcoredesign-is-now-a-developmentdependency-package) so you need to modify your project file:

    If you need to reference this package to override EF Core's design-time behavior, then you can update PackageReference item metadata in your project.

    
      all
      
      
    
    

提交回复
热议问题