DuckDbDateOnlyToDateTimePropertyHandler
A property handler that maps a DuckDB DATE column onto a DateTime property.
A property handler that maps a DuckDB DATE column onto a DateTime property. DuckDB.NET returns a DateOnly when reading a DATE value, and its compiled reader casts values via the reader’s strongly-typed accessors, so a DateTime-typed property has no direct match against the returned DateOnly without this handler.
Overview
| Name | Description |
|---|---|
| Class | DuckDbDateOnlyToDateTimePropertyHandler |
| Namespace | RepoDb.PropertyHandlers.DuckDb |
| Package | RepoDb.DuckDb |
| Implements | IPropertyHandler<object, DateTime> |
| Column type | The DuckDB DATE type |
| Property type | DateTime |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Converts the DateOnly value returned by the driver into a DateTime at midnight. Returns default when the value is not a DateOnly. |
| Set | Property to database | Converts the DateTime into a DateOnly, to be written into a DATE column. The time-of-day component is ignored. |
Use DuckDbDateOnlyToNullableDateTimePropertyHandler for a nullable
DateTime?property.
Usability
Bind the handler to the property that maps to a DATE column.
Attribute
public class Person
{
public int Id { get; set; }
[Map("birth_date"), PropertyHandler(typeof(DuckDbDateOnlyToDateTimePropertyHandler))]
public DateTime BirthDate { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new DuckDBConnection(ConnectionString))
{
connection.Insert(new Person { Id = 1, BirthDate = new DateTime(1990, 5, 20) });
var person = connection.Query<Person>(e => e.Id == 1).First();
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Person>()
.PropertyHandler<DuckDbDateOnlyToDateTimePropertyHandler>(e => e.BirthDate);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Person, DuckDbDateOnlyToDateTimePropertyHandler>(e => e.BirthDate, new DuckDbDateOnlyToDateTimePropertyHandler(), true);