简体   繁体   中英

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; }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM