How can I map .NET DateTime to database DateTime2 using Dapper to avoid “SqlDateTime overflow” exception?

There’s a much easier solution now in a similar question, but it is about string.

For DateTime:

SqlMapper.AddTypeMap(typeof(DateTime), System.Data.DbType.DateTime2);

This must be applied before any database call like INSERT.

You can put it in your Main or Startup class, or any place that runs on Startup to configure data access. SqlMapper is a static class and changes apply to all calls.

Leave a Comment