Make T4 reverse POCO generate classes properly

177 Views Asked by At

I am using a project which has T4MVC generating classes from a database table. I have several issues but this seems to be a pattern with them. I have not posted the .tt file because it is pretty large.

for example HasRequired(a => a.AccountPaymentSettingId).WithMany(b => b.CustomerFileTypeAccountPaymentSettingFlexibles).HasForeignKey(c => c.AccountPaymentSettingId);

has a => a.AccountPaymentSettingId and I want a => a.AccountPaymentSetting

Here is the top of the .tt file to show the version

<#@ include file="..\\EF.Reverse.POCO.Core.ttinclude" #>
<#
    // v2.17.1

It is not using desired names in the generated code.

This is what is generated:

    public CustomerFileTypeAccountPaymentSettingFlexibleConfiguration(string schema)
    {
        ToTable(schema + ".Customer_FileType_Account_Payment_Setting_Flexible");
        HasKey(x => x.AccountPaymentSettingFlexibleId);

        Property(x => x.AccountPaymentSettingFlexibleId).HasColumnName("Account_Payment_Setting_Flexible_ID").IsRequired().HasColumnType("int").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.AccountPaymentSettingId).HasColumnName("Account_Payment_Setting_ID").IsRequired().HasColumnType("int");
        Property(x => x.PaymentMethod).HasColumnName("Payment_Method").IsRequired().IsUnicode(false).HasColumnType("varchar").HasMaxLength(5);
        Property(x => x.Priority).HasColumnName("Priority").IsRequired().HasColumnType("int");
        Property(x => x.RowCreatedDate).HasColumnName("RowCreatedDate").IsRequired().HasColumnType("datetime2");

        HasRequired(a => a.AccountPaymentSettingId).WithMany(b => b.CustomerFileTypeAccountPaymentSettingFlexibles).HasForeignKey(c => c.AccountPaymentSettingId);
        HasRequired(a => a.PaymentMethod).WithMany(b => b.CustomerFileTypeAccountPaymentSettingFlexibles).HasForeignKey(c => c.PaymentMethod);
        InitializePartial();
    }

and this is my desired results:

    public CustomerFileTypeAccountPaymentSettingFlexibleConfiguration(string schema)
    {
        ToTable(schema + ".Customer_FileType_Account_Payment_Setting_Flexible");
        HasKey(x => x.AccountPaymentSettingFlexibleId);

        Property(x => x.AccountPaymentSettingFlexibleId).HasColumnName("Account_Payment_Setting_Flexible_ID").IsRequired().HasColumnType("int").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.AccountPaymentSettingId).HasColumnName("Account_Payment_Setting_ID").IsRequired().HasColumnType("int");
        Property(x => x.PaymentMethodCode).HasColumnName("Payment_Method").IsRequired().IsUnicode(false).HasColumnType("varchar").HasMaxLength(5);
        Property(x => x.Priority).HasColumnName("Priority").IsRequired().HasColumnType("int");

        HasRequired(a => a.AccountPaymentSetting).WithMany(b => b.CustomerFileTypeAccountPaymentSettingFlexibles).HasForeignKey(c => c.AccountPaymentSettingId);
        HasRequired(a => a.PaymentMethod).WithMany(b => b.CustomerFileTypeAccountPaymentSettingFlexibles).HasForeignKey(c => c.PaymentMethodCode);
        InitializePartial();
    }

Just for completeness, this is the relevant part of the table (I left off some default and FK constraints to keep it compact)

CREATE TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible](
    [Account_Payment_Setting_Flexible_ID] [int] IDENTITY(1,1) NOT NULL,
    [Account_Payment_Setting_ID] [int] NOT NULL,
    [Payment_Method] [varchar](5) NOT NULL,
    [Priority] [int] NOT NULL,
    [RowCreatedDate] [datetime2](7) NOT NULL,
 CONSTRAINT [PK_Customer_FileType_Account_Payment_Setting_Flexible] PRIMARY KEY CLUSTERED 
(
    [Account_Payment_Setting_Flexible_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PayspanHealth_Data3],
 CONSTRAINT [UK_CustomerFileTypeAccountPaymentSettingFlexible_SettingId_PaymentMethod] UNIQUE NONCLUSTERED 
(
    [Account_Payment_Setting_ID] ASC,
    [Payment_Method] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PayspanHealth_Data3],
 CONSTRAINT [UK_CustomerFileTypeAccountPaymentSettingFlexible_SettingId_Priority] UNIQUE NONCLUSTERED 
(
    [Account_Payment_Setting_ID] ASC,
    [Priority] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PayspanHealth_Data3]
) ON [PayspanHealth_Data3]
GO


ALTER TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible] ADD  CONSTRAINT [DF_CustomerFileTypeAccountPaymentSettingFlexible_RowCreatedDate]  DEFAULT (sysdatetime()) FOR [RowCreatedDate]
GO

ALTER TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible]  WITH CHECK ADD  CONSTRAINT [FK_CustomerFileTypeAccountPaymentSettingFlexible_AccountPaymentSettingId] FOREIGN KEY([Account_Payment_Setting_ID])
REFERENCES [dbo].[Customer_FileType_Account_Payment_Settings] ([Account_Payment_Setting_ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible] CHECK CONSTRAINT [FK_CustomerFileTypeAccountPaymentSettingFlexible_AccountPaymentSettingId]
GO

ALTER TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible]  WITH CHECK ADD  CONSTRAINT [FK_CustomerFileTypeAccountPaymentSettingFlexible_PaymentMethod] FOREIGN KEY([Payment_Method])
REFERENCES [dbo].[Payment_Method] ([Payment_Method_Code])
GO

ALTER TABLE [dbo].[Customer_FileType_Account_Payment_Setting_Flexible] CHECK CONSTRAINT [FK_CustomerFileTypeAccountPaymentSettingFlexible_PaymentMethod]
GO
0

There are 0 best solutions below