Search code examples
asp.net-coreentity-framework-coreasp.net-core-mvcnpgsqlasp.net-mvc-scaffolding

Scaffold common base class for all entities without discriminator


Scaffold creates Poco classes like

namespace MyDbContext
{
    public partial class Customer
    {
        public Customer()
        {
        }

        public string Id { get; set; }
        public string Name { get; set; }
   }
}

How to force it to generate common base class for all entities like

    public partial class Customer : EntityBase
    { }

Inheritance generates discriminator field. EntityBase class not have database properties freom database. So discriminator field generation is useless. How to disable discriminator field generation ?

Scaffold is perfomed at runtime. So it it not possible to manually change created code or add partial classes for every table. Docs in https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding does not describe such option in

dotnet ef dbcontext

command.

This is ASP.NET 5 MVC application using Npgsql EF Core data provider.


Solution

  • Simple thinking. no reversed engineering can detect common part of your entities. More, assumption you have many entities: E1, E2, E3, E4. When you scaffold E1, E2, E3 at one time, then you scaffold E4 at second time, how to detect mutual part of first time and second time.

    Therefore, logical thinking shows you that has no tool as you expected.

    You can done your task manually:

    • Generate entity POCO automatically
    • Create a base class
    • Delete properties what are mutually
    • Extend from base class