Entity Framework Core Customize Scaffolding

前端 未结 5 1669
天命终不由人
天命终不由人 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:40

    @Tornike Choladze's excellent answer led me in the right direction, but in the latest versions of .Net Core (2.0 >) this has to be done a little differently it seems, with regard to the setup.

    The custom entity type generator:

    class MyEntityTypeGenerator : CSharpEntityTypeGenerator
    {
        public MyEntityTypeGenerator(ICSharpUtilities cSharpUtilities) : base(cSharpUtilities) { }
    
        public override string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations)
        {
            string code = base.WriteCode(entityType, @namespace, useDataAnnotations);
    
            var oldString = "public partial class " + entityType.Name;
            var newString = "public partial class " + entityType.Name + " : EntityBase";
    
            return code.Replace(oldString, newString);
        }
    }
    

    And the setup, which consists of a class in the same assembly and implementing IDesignTimeServices:

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

提交回复
热议问题