Getting intermittent InvalidCastException when using Firebird and Entity Framework with Data Context

117 views Asked by At

I'm seeing the following error in my logs when calling one of my endpoints:

An exception occurred while iterating over the results of a query for context type 'projectname.Data.DataContext'. System.InvalidCastException: Object cannot be cast from DBNull to other types. at System.DBNull.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value, IFormatProvider provider)
at FirebirdSql.Data.Common.DbValue.GetDateTime() at FirebirdSql.Data.FirebirdClient.FbDataReader.GetFieldValue[T](Int32 i) at FirebirdSql.Data.FirebirdClient.FbDataReader.GetDateTime(Int32 i)
at lambda_method12(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() System.InvalidCastException: Object cannot be cast from DBNull to other types. at System.DBNull.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value, IFormatProvider provider)
at FirebirdSql.Data.Common.DbValue.GetDateTime() at FirebirdSql.Data.FirebirdClient.FbDataReader.GetFieldValue[T](Int32 i) at FirebirdSql.Data.FirebirdClient.FbDataReader.GetDateTime(Int32 i)
at lambda_method12(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

The line of code it's dying on is at the end of the snippet:

public async Task<ServiceResponse<string>> Login(string username, string password)
{
    var response = new ServiceResponse<string>();
    var user = await _context.Users.FirstOrDefaultAsync(u => u.Username.ToLower().Equals(username.ToLower()));
...

Oddly, my User model has an element DateTime LastLogin that if I comment out, the endpoint works without any issues. There are other DateTime elements and I was able to leave those alone.

The query that entity framework runs is the following:

SELECT "u"."USERID", "u"."CONFIRMTOKENGENTIME", "u"."CONFIRMATIONTOKEN", "u"."CREATEDBY", "u"."CREATEDON", "u"."EMAILADDRESS", "u"."HASHALGORITHMID", "u"."ISACTIVE", "u"."ISDELETED", "u"."ISLOCKED", "u"."LASTLOGIN", "u"."MODIFIEDBY", "u"."MODIFIEDON", "u"."PASSWORDHASH", "u"."PASSWORDRECOVERYTOKEN", "u"."PASSWORDSALT", "u"."PASSWORDTOKENGENTIME", "u"."USERNAME"
FROM "USER_LOGIN_INFO" AS "u"
WHERE LOWER("u"."USERNAME") = CAST(@__ToLower_0 AS VARCHAR(8191))
ROWS (1)

which if I throw into IBExpert, it returns a result (assuming I put in a valid username).

I'm currently running .Net 6.0.400 and upgraded Entity Framework to 6.0.4 as well.

Any ideas of what I may be running into would be great.

0

There are 0 answers