Table-per-type inheritance with the Entity Framework Reverse POCO Generator

989 views Asked by At

Is it possible to use the (awesome) EntityFramework Reverse POCO Generator to generate POCOs in a table-per-type inheritance scenario?

My database contains a 'base' log table, and two tables that derive from it:

create table LogBase
(
    Id int identity(1, 1) not null,
    LogTime datetime not null default getdate(),
    constraint PK_LogBase primary key clustered(Id)
) 

create table ErrorLog
(
    Id int not null,
    ErrorMessage nvarchar(max),
    StackTrace nvarchar(max),
    constraint PK_ErrorLog primary key(Id),
    constraint FK_ErrorLog_LogBase foreign key(Id) references LogBase(Id)
)

create table ChangeLog
(
    Id int not null,
    PropertyName nvarchar(max),
    OldValue nvarchar(max),
    NewValue nvarchar(max),
    constraint PK_ChangeLog primary key(Id),
    constraint FK_ChangeLog_LogBase foreign key(Id) references LogBase(Id)
)

By default, the Reverse POCO Generator generates 3 C# classes - LogBase, ErrorLog, and ChangeLog - each of which contains an Id property, and which have no inheritance relationship with each other.

I can specify that ErrorLog and ChangeLog inherit from LogBase by creating the classes as partials and putting the : LogBase inheritance in the partial classes - is this the correct way to specify the inheritance?

In the template generator, the UpdateColumn callback allows me to specify tables that should omit their Id columns in the generated POCO.

I can use UpdateColumn for the ErrorLog and ChangeLog tables - this results in the 'Id' property being dropped from each class, which is correct for table-per-type inheritance. However, it also results in the ErrorLog and ChangeLog classes being removed from the generated DbContext, and the following comment appears in the ErrorLog and ChangeLog classes:

// The table 'ChangeLog' is not usable by entity framework because it
// does not have a primary key. It is listed here for completeness.
  • Is there a way to specify the inheritance relationship without causing the generator to omit the derived tables from the model?

  • Is there a way of preventing the generator from including navigation properties in the generated POCOs?

2

There are 2 answers

0
Simon Hughes On BEST ANSWER

The latest release (5th Jan 2017) in v2.27 has fixed an issue (#167) with hidden columns and primary keys making this work now.

Any problems, update the GitHub case over at https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/issues/181

1
ErikEJ On