クエリ結果をマップしようとしている「モデル」がありますが、何らかの理由で失敗し続けます。ここにいくつかの詳細情報があります:
ここで例外が発生します (クエリの選択部分以外はすべて実際には異なります)。
var query = @"
        SELECT 
            Id,
            PublicationDate,  
            Title,  
            IntroText,  
            BodyText,  
            IsReadByTarget,
            IsRequired
        FROM Notifications
        WHERE 
            CategoryId = @categoryId
        ";
var parameters = new Dictionary<string, object> {
    { "@categoryId", AppSettings.NotificationCategoryId },
};
var notifications = SqlHelper.GetList<Notification>(_connectionString, query, parameters);
SqlHelper は、すべてのマッピングを行う小さなヘルパー クラスです。通知は、私がマッピングしているモデルです。これは次のようになります。
public class Notification
{
    public string Id { get; set; }
    public Time PublicationDate { get; set; }
    public string Title { get; set; }
    public string IntroText { get; set; }
    public string BodyText { get; set; }
    public string ActiveText
    {
        get
        {
            return string.IsNullOrEmpty(IntroText) ? BodyText : IntroText;
        }
    }
    public Notifiable Target { get; set; }
    public bool IsReadByTarget { get; set; }
    public bool IsRequired { get; set; }
}
時間もカスタムクラスです。基本的に、日付と時刻を保持します (datetime と同じですが、はるかに小さい)。計算などではなく、通信にのみ使用されます。
public class Time
{
    public int Year { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }
    public int Hour { get; set; }
    public int Minute { get; set; }
    public int Second { get; set; }
    public Time()
        : this(DateTime.Now)
    {
    }
    public Time(DateTime time)
    {
        Year = time.Year;
        Month = time.Month;
        Day = time.Day;
        Hour = time.Hour;
        Minute = time.Minute;
        Second = time.Second;
    }
    public static implicit operator DateTime(Time time)
    {
        return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second);
    }
    public static implicit operator Time(DateTime dateTime)
    {
        return new Time(dateTime);
    }
}
ここから魔法が始まります。ご覧のとおり、DateTime から Time へ、および Time から DateTime へサイレントに変換する必要があります。これは、通常の場合は正常に機能します。だから次のようなことをして...
Time myTime = DateTime.Now;
...正常に動作します。
しかし、私の場合、次のようになります。
「System.DateTime」から「MyNamespace.Time」へのキャストが無効です。
public static List<T> GetList<T>(string connectionString, string query, Dictionary<string, object> parameters) where T : class, new()
{
    var data = new List<T>();
    using (var conn = new SqlConnection(connectionString))
    {
        conn.Open();
        using (var command = conn.CreateCommand())
        {
            command.CommandText = query;
            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    command.Parameters.AddWithValue(parameter.Key, parameter.Value);
                }
            }
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var item = Read<T>(reader);
                    data.Add(item);
                }
            }
        }
    }
    return data;
}
public static T Read<T>(SqlDataReader reader) where T : new()
{
    var item = new T();
    var properties = typeof(T).GetProperties();
    foreach (var propertyInfo in properties)
    {
        if (!reader.HasColumn(propertyInfo.Name)) continue;
        var ordinal = reader.GetOrdinal(propertyInfo.Name);
        if (reader.IsDBNull(ordinal)) continue;
        propertyInfo.SetValue(item, Convert.ChangeType(reader[ordinal], propertyInfo.PropertyType), null);
    }
    return item;
}
したがって、基本的には、DateTime 列を Time オブジェクトにマッピングし、DateTime オブジェクトにマッピングするとうまくいきません。なぜこれが起こるのかについての助けと、合理的な修正をいただければ幸いです。
Time の代わりに DateTime を使用する新しいモデルを作成してそれにマップし、そのモデルを Time を使用してモデルにマップできることはわかっていますが、それは合理的な修正ではありません。