0

How can I configure T4 POCO generation in Linq2Db to generate models that use NodaTime types instead of System.DateTime?

I'm using PostgreSQL with Npgsql.

zigzag
  • 579
  • 5
  • 17

1 Answers1

1

To substitute standard DateTime classes you have to modify your T4 template in the following way:

// loading database schema
LoadPostgreSQLMetadata(...)

// modifying default mapping
foreach (var t in Tables.Values)
{
    foreach (var c in t.Columns.Values)
    {
        switch (c.Type)
        {
            case "DateTime"       : c.Type = "NodaTime.LocalDateTime";   break;
            case "DateTime?"      : c.Type = "NodaTime.LocalDateTime?";  break;
            case "DateTimeOffset" : c.Type = "NodaTime.OffsetDateTime";  break;
            case "DateTimeOffset?": c.Type = "NodaTime.OffsetDateTime?"; break;
        }
    }
}

// generating model
GenerateModel();
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
  • This works. Thank you! I added `GetSchemaOptions.PreferProviderSpecificTypes = true;` before `LoadPostgreSQLMetadata(...)` call, to get more granular control over date and time types, and adjusted mappings accordingly. – zigzag May 05 '21 at 13:28