locked
Scaffold common base class for all entities RRS feed

  • Question

  • User-1262787652 posted
    Creating ASP.NET 5 Core MVC application using Npgsql EF Core provider.
    
    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
            { }
    
    or generic base class
    
          public partial class Customer : EntityBase<Customer>
            { }
    
    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://docs.microsoft.com/en-us/ef/core/managing-schemas/scaffolding does not describe such option in
    
        dotnet ef dbcontex
    
    command.

    Monday, May 17, 2021 7:20 AM

All replies

  • User475983607 posted

    kobruleht

    Reply to: Scaffold common base class for all entities

    Inheritance causes generates a discriminator field.  Your table design will have a discriminator fields   You should be very interested in reading the official documentation to learn the basics https://docs.microsoft.com/en-us/ef/core/modeling/inheritance.

    Your code example does not make any logical sense or follow C# standards.

    Monday, May 17, 2021 10:30 AM
  • User-1262787652 posted

    Hi!

    EntityBase does not have database properties freom database. So discriminator field generation is useless.

    How to disable discriminator generation ?

    Monday, May 17, 2021 10:39 AM
  • User475983607 posted

    EntityBase does not have database properties freom database. So discriminator field generation is useless.

    How to disable discriminator generation ?

    Maybe you are looking for extension methods for the DbSet type???   Extension method allows you to add methods to an existing class without inheritance or modifying the class.

    Unfortunately, your requirement and sample code are difficult to understand. 

    Monday, May 17, 2021 11:15 AM