2

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.

Arioch 'The
  • 15,799
  • 35
  • 62
CSharpNewb
  • 181
  • 5
  • 15
  • 1
    I think that the value of the column is NULL in the database and that is causing this exception. Can you cross check the value stored in the DB – Vishal Anand Aug 26 '22 at 04:42
  • 3
    This is a guess, EF is assuming that a datetime column can't be null, but it is. You probably need to change your model class to `DateTime?`. – Jeremy Lakeman Aug 26 '22 at 04:47
  • try http://fbprofiler.sf.net or any other TraceAPI client to see which request your C# app sends to the server, and which data are returned. IBExpert has it too, but i think it's tracing UI is not functional – Arioch 'The Aug 26 '22 at 20:22

0 Answers0