#Make better SQLite DateTime convert Microsoft.EntityFramework

1 messages ยท Page 1 of 1 (latest)

midnight epoch
#

I'm getting:
ArgumentOutOfRangeException: Valid values are between 0 and 999, inclusive. (Parameter 'millisecond')
System.DateTime.ThrowMillisecondOutOfRange()

from DbContext, in model of DbSet<>, I have property public DateTime CreatedAt { get; set; }
in .db table I set valid date, but when I try to get it from model, I'm getting exception

wheat wigeon
#

iirc I got shouted at (not literally) for using DateTime in my database fields. Try DateTimeOffset, but from what I understand, the correct approach for times in databases is Nodatime.

#

Also, I don't think this is a Discord.Net related question. ๐Ÿ˜›

wheat wigeon
#

oh ๐Ÿ˜›

midnight epoch
wheat wigeon
#

Where's the error being thrown? What line?

#

That's not a db error so I assume you're retrieving the date from the database?

midnight epoch
wheat wigeon
#

That date is Unix Epoch

#

I think Epoch is long? What's the database type?

midnight epoch
wheat wigeon
#

What database is this? Just curious, unrelated question

#

Can you show the stacktrace for where it fails?

#

In the codeblock you gave me I don't see anything related to

ArgumentOutOfRangeException: Valid values are between 0 and 999, inclusive. (Parameter 'millisecond')
System.DateTime.ThrowMillisecondOutOfRange()
wheat wigeon
#

All good. Also I double checked, UnixEpoch uses DateTime type. We just need to double check how you're reading from the database.

midnight epoch
# wheat wigeon Can you show the stacktrace for where it fails?
An unhandled exception occurred while processing the request.
ArgumentOutOfRangeException: Valid values are between 0 and 999, inclusive. (Parameter 'millisecond')

System.DateTime.ThrowMillisecondOutOfRange()

ArgumentOutOfRangeException: Valid values are between 0 and 999, inclusive. (Parameter 'millisecond')

    System.DateTime.ThrowMillisecondOutOfRange()
    System.DateTime..ctor(int year, int month, int day, int hour, int minute, int second, int millisecond)
    Microsoft.Data.Sqlite.SqliteValueReader.FromJulianDate(double julianDate)
    Microsoft.Data.Sqlite.SqliteValueReader.GetDateTime(int ordinal)
    Microsoft.Data.Sqlite.SqliteDataReader.GetDateTime(int ordinal)
    lambda_method61(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
    Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+Enumerator.MoveNext()
    System.Collections.Generic.LargeArrayBuilder<T>.AddRange(IEnumerable<T> items)
    System.Collections.Generic.EnumerableHelpers.ToArray<T>(IEnumerable<T> source)
    System.Linq.Enumerable.ToArray<TSource>(IEnumerable<TSource> source)
    ArraBruh.Controllers.ReviewsController.Reviews() in ReviewsController.cs

            return View(reviews.ToArray());

lambda_method59(Closure , object , object[] )
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
wheat wigeon
#

Go to where that line shows that error and show the code related.

#

I assume this isn't your project or you're trying to fix someone else's ๐Ÿ˜„

midnight epoch
wheat wigeon
#

Can you show the object properties for Users?

#

I'm honestly a bit stuck here tbf. I don't quite understand where the implicit time conversion is happening.

#

Assuming that is the code at fault.

manic ledge
#

what does userreview.fromdata do

midnight epoch
midnight epoch
wheat wigeon
#

oh, what object is the DateTime part of?

#

Or which context more specifically

midnight epoch
# manic ledge what does userreview.fromdata do
public static IUserReview FromData(Review data, IUserAccount userAccount)
{
    return new UserReview(
        userAccount,

        data.Id,
        data.Text,

        data.IsEdited,
        data.IsDeleted,

        data.EditedAt,
        data.DeletedAt,
        data.CreatedAt);
}```
midnight epoch
manic ledge
#

i forget what its called but what is the binding for the db model in EF config? like the code that generates the conversion from & to the database

wheat wigeon
midnight epoch
manic ledge
#

do you have anything like that?

midnight epoch
midnight epoch
#

strange, that I ran into a problems only with dates, maybe with another complex data types will be same ;)

wheat wigeon
#

Can you comment out that code you have and just pull the entire object where the Ids match for trying to identify the problem.

midnight epoch
wheat wigeon
#

Ya, just assign it something.

midnight epoch
midnight epoch
wheat wigeon
#
var reviewer = m_dbContext.Reviews.FirstOrDefault(x => x.Id == id);
reviewer.CreatedAt; 

See what the type it thinks it is.

#

idk what your arg passthrough is for Id, but just pull 1 record

manic ledge
midnight epoch
#

just as I say

manic ledge
#

it might not know how to convert a sqlite datetime back to a .net datetime

wheat wigeon
#

Weird. I don't have enough C# experience to know where the issue is tbf. But, something between the Type in your model and the database type is going all screwey.

midnight epoch
#
at System.DateTime.ThrowMillisecondOutOfRange()
   at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond)
   at Microsoft.Data.Sqlite.SqliteValueReader.FromJulianDate(Double julianDate)
   at Microsoft.Data.Sqlite.SqliteValueReader.GetDateTime(Int32 ordinal)
   at Microsoft.Data.Sqlite.SqliteDataReader.GetDateTime(Int32 ordinal)```I guess it throws betwen db model and db
midnight epoch
wheat wigeon
#

Can you get one object from the set so we can analyse that? We don't need the entire set. I kinda want to try and set the Model to be a long then using DateTimeOffset.FromUnixTimeSeconds(inTme out var theTime)

#

but idk if this will work since I think it's the mapping that's kinda broken? ๐Ÿ˜„

midnight epoch
wheat wigeon
#

Scuffed workaround ๐Ÿ˜„ definitely going to bite you in the future

#

Maybe someone more knowledgable can help with a better solution. ๐Ÿ˜„

midnight epoch
#

Make better SQLite DateTime convert Microsoft.EntityFramework

wheat wigeon
# midnight epoch anyway, thx <3

Just a thought. Was the type ever changed in from when data was submitted and now?

I may be entirely wrong but I don't see any easy way to get UnixEpoch time? https://learn.microsoft.com/en-us/dotnet/api/system.datetime.unixepoch?view=net-7.0

The only way I see to get the Epoch time it to kinda go out of the way to get it. ๐Ÿ˜„

(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;

Given this, then the correct field type would be a long and not DateTime.

midnight epoch
#

DateTimeOffset.UtcNow.DateTime - will return DateTime of it