DarkWanderer / ClickHouse.Client

.NET client for ClickHouse
MIT License
321 stars 66 forks source link

BulkCopy: Failing to convert DateOnly to DbType Date32 #501

Closed himanshusaini111 closed 2 months ago

himanshusaini111 commented 3 months ago

Model:

public class Model
{
    .
    .
    public DateOnly date { get; set; }
    .
    .
}

Exception Message:

Unable to cast object of type 'System.Int64' to type 'System.DateTime'.

StackTrace:

at ClickHouse.Client.Types.Date32Type.Write(ExtendedBinaryWriter writer, Object value) at ClickHouse.Client.Copy.ClickHouseBulkCopy.SerializeBatch(Batch batch)

Checked the Source Code:

https://github.com/DarkWanderer/ClickHouse.Client/blob/main/ClickHouse.Client/Types/Date32Type.cs

        var sinceEpoch = ((DateTime)value).Date - DateTimeEpochStart;
        writer.Write(Convert.ToInt32(sinceEpoch.TotalDays));

It Seems like we are Casting the value in DateTime without any backup plan.

DarkWanderer commented 3 months ago

Can you try version 7.6.1?