If I have an object such as:
public class Person
{
public int Id {get;set;}
public string Name {get;set;}
public DateTime DOB {get;set;}
}
If I set the name on my object and DOB is DateTime.MinValue and use Dapper like so:
INSERT INTO [Person] ([Person].[Name], [Person].[DOB]) VALUES (@Name, @DOB);
SELECT CAST(SCOPE_IDENTITY() AS BIGINT) AS [Id]
connection.Query<long>(sql, entity);
This throws SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
However if I execute the SQL in SQL Management Studio with a string version '0001-01-01 00:00:00' it inserts into the db fine.
Any ideas how to get this to work?
Thanks
UPDATE:
CREATE TABLE [dbo].[Person](
[Id] [int] IDENTITY(1,1) NOT NULL,
[DOB] [datetime2](7) NULL,
[Name] [nvarchar](20) NOT NULL,
CONSTRAINT [PK_Referrer_Referee] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
.Net DateTime should handle both.
Dapper is mapping datetime to DbType DateTime and not DbType.DateTime2 which is what you need.
https://github.com/SamSaffron/dapper-dot-net/blob/master/Dapper%20NET40/SqlMapper.cs#L384
typeMap[typeof(DateTime)] = DbType.DateTime; typeMap[typeof(DateTime?)] = DbType.DateTime;
But you can add a TypeMap yourself but then you need to create a datetime2 type/class.