mikependon / RepoDB

A hybrid ORM library for .NET.
Apache License 2.0
1.68k stars 122 forks source link

Does RepoDB support the TimeOnly data type? #1165

Closed gregoryagu closed 7 months ago

gregoryagu commented 7 months ago

I have a Project which uses Sql Server and the TimeOnly data type.

When I attempt to save an entity which has a TimeOnly field, I get the following error message. I searched the docs, but I did not find any mention of TimeOnly, Is there a way to resolve this?

Using version 1.13.0

await connection.UpdateAsync(tutorInfo);

System.InvalidCastException HResult=0x80004002 Message=Failed to convert parameter value from a TimeOnly to a TimeSpan. Source=Microsoft.Data.SqlClient StackTrace: at Microsoft.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming) at Microsoft.Data.SqlClient.SqlParameter.GetCoercedValue() at Microsoft.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) at Microsoft.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue) at Microsoft.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method) at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName) at Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQueryInternal(CommandBehavior behavior, AsyncCallback callback, Object stateObject, Int32 timeout, Boolean inRetry, Boolean asyncWrite) at Microsoft.Data.SqlClient.SqlCommand.BeginExecuteNonQueryAsync(AsyncCallback callback, Object stateObject) at System.Threading.Tasks.TaskFactory1.FromAsyncImpl(Func3 beginMethod, Func2 endFunction, Action1 endAction, Object state, TaskCreationOptions creationOptions) at System.Threading.Tasks.TaskFactory1.FromAsync(Func3 beginMethod, Func2 endMethod, Object state) at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQueryAsync(CancellationToken cancellationToken) --- End of stack trace from previous location --- at RepoDb.DbConnectionExtension.<UpdateAsyncInternalBase>d__10081.MoveNext() at RepoDb.DbConnectionExtension.d989`1.MoveNext() at Rihisi.Data.DataStore.d0.MoveNext() in C:\Dev\BlueHatSoftware\Rihisi\V5\Rihisi\Rihisi\Data\DataStore.cs:line 25

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidCastException: Object must implement IConvertible.

gregoryagu commented 7 months ago

I was able to get this working by defining the Entity Type as a TimeSpan, instead of a TimeOnly. Then no exception was thrown. I then convert it to a TimeOnly.

public TimeSpan TimeOnlyTest {get;set;}

public TimeOnly MyTimeOnly
{
    get { return TimeOnly.FromTimeSpan(this.TimeOnlyTest); }
    set { this.TimeOnlyTest = value.ToTimeSpan(); }
}