sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
706 stars 230 forks source link

How to debug DateTime defaults not rendered #758

Closed lukos closed 2 years ago

lukos commented 2 years ago

We have some code that sets defaults on all columns that don't have defaults set in the database e.g. "" for string, DateTime.Now for datetime.

However, when I run the template, all of these defaults get rendered in the POCO class constructor except for the DateTime ones. This is the case regardless of whether they have a default in the database, which is usually getdate() or not, in which case we are setting the default to DateTime.Now. I can see the code that takes care of mapping getdate() to DateTime.Now so I'm unsure why these wouldn't render and how to debug the template to try and work it out.

sjh37 commented 2 years ago

This should work fine.

with

CREATE TABLE footer
(
    ID INT NOT NULL IDENTITY(1, 1),
    otherID INT NOT NULL,
    added DATETIME NOT NULL DEFAULT GETDATE(),
    CONSTRAINT PK_footer PRIMARY KEY (Id)
);

I get a class defined as

public class Footer
{
    public int Id { get; set; } // ID (Primary key)
    public int OtherId { get; set; } // otherID
    public DateTime Added { get; set; } // added

    public Footer()
    {
        Added = DateTime.Now;
    }

Could you show me the SQL used to create your table.

lukos commented 2 years ago

They are all pretty simple tables, the following one has no defaults but should have been given them in code (I have removed a load of the irrelevant columns):

CREATE TABLE [dbo].[Tickets](
    [TicketID] [int] IDENTITY(1,1) NOT NULL,
    [SiteID] [int] NULL,
    [DateAdded] [datetime] NULL,
    [DateUpdated] [datetime] NULL,
 CONSTRAINT [PK_Tickets] PRIMARY KEY CLUSTERED 
(
    [TicketID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

This next one does have defaults but is also not rendering the column at all:

CREATE TABLE [dbo].[AdminLog](
    [AdminLogId] [int] IDENTITY(1,1) NOT NULL,
    [SiteId] [int] NULL,
    [ChaseDate] [datetime] NULL,
    [ChaseDueDate] [datetime] NULL,
    [DateAdded] [datetime] NULL,
    [DateUpdated] [datetime] NULL,
 CONSTRAINT [PK_AdminLog] PRIMARY KEY CLUSTERED 
(
    [AdminLogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[AdminLog] ADD  CONSTRAINT [DF_AdminLog_DateAdded]  DEFAULT (getdate()) FOR [DateAdded]
GO

ALTER TABLE [dbo].[AdminLog] ADD  CONSTRAINT [DF_AdminLog_DateUpdated]  DEFAULT (getdate()) FOR [DateUpdated]
GO
sjh37 commented 2 years ago
// Tickets
public class Ticket
{
    public int TicketId { get; set; } // TicketID (Primary key)
    public int? SiteId { get; set; } // SiteID
    public DateTime? DateAdded { get; set; } // DateAdded
    public DateTime? DateUpdated { get; set; } // DateUpdated
}

// AdminLog
public class AdminLog
{
    public int AdminLogId { get; set; } // AdminLogId (Primary key)
    public int? SiteId { get; set; } // SiteId
    public DateTime? ChaseDate { get; set; } // ChaseDate
    public DateTime? ChaseDueDate { get; set; } // ChaseDueDate
    public DateTime? DateAdded { get; set; } // DateAdded
    public DateTime? DateUpdated { get; set; } // DateUpdated

    public AdminLog()
    {
        DateAdded = DateTime.Now;
        DateUpdated = DateTime.Now;
    }
}
sjh37 commented 2 years ago

What version are you on?

lukos commented 2 years ago

It is v3.6.0 of the VS extension running on VS2022. I have paused this work for now since I don't need it and will re-look later. I might be able to debug the template and try and work out what is going on. Thanks