Vinícius Filenga
Vinícius Filenga

Reputation: 21

Dapper brings datetime field as default value

How come all my searches with dapper bring an empty (default Datetime value) for this workoutdate field?

StringBuilder query = new StringBuilder();

query.Append("SELECT W.WorkoutId, W.Active, W.GymId, W.Spots, W.UserId, W.WorkoutDate, W.WorkoutStatusId FROM [Workout] W");

    public class Workout
    {
        public Guid WorkoutId { get; private set; }
        public Guid UserId { get; set; }
        public Guid? GymId { get; set; }
        public int WorkoutStatusId { get; set; }
        public int Spots { get; set; }
        public bool Active { get; set; }
        public DateTime WorkoutDate { get; set; }
    }

Upvotes: 0

Views: 2788

Answers (1)

Palle Due
Palle Due

Reputation: 6312

Your column is a datetime2. You can change the way Dapper maps columns like this:

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

An alternative is to cast it in the select like this:

SELECT CAST(W.WorkoutDate AS datetime) ...

Or you could change your column to datetime, if you don't need the added precision of datetime2.

Upvotes: 1

Related Questions