Skip to content

Different DateTimeKind per property or database #1419

@vincentsels

Description

@vincentsels

When all DateTimes are stored using the same DateTimeKind, you can easily set the correct kind by using a DateTimeHandler : SqlMapper.TypeHandler<DateTime> and configuring that somewhere through the static SqlMapper.AddTypeHandler. This will then be applied for all mappings.

It would be useful if there were a way to configure this on a property-per-property basis, or at least per database connection.

One way to do this would be by passing more context to the TypeHandler's SetValue and Parse methods. Passing the name of the property would already help - we could then e.g. scan for a 'Utc'- or 'Local'-suffix. Better yet would be passing any attributes set to the property. This would be a very generic approach, not only usable for this use case.

Dapper could then include its own attribute for this, e.g. DateTimeKindAttribute, which is used by the default implementation of the DateTime TypeHandler.

Of course, this assumes it's possible to reflect on the property; and given Dapper's focus on speed, I'm not sure if that's feasible.

I'm using Dapper v2.0.30 and .NET Framework v4.2.6.

Related to #571 - but that issue isn't about supporting different DateTimekinds.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions