6 Replies Latest reply on Jun 15, 2020 4:52 PM by Alex Keh - Product Manager-Oracle

    Using DateTime.Today breaks with Entity Framework Core 3.1

    3799577

      Hi,

       

      I have the following query:

       

      var users = await oracleContext

          .Personnes

          .Where(u => u.Service == "SI" && (u.Floor == "06" || u.Floor == "07") && (u.ExitDate == null || u.ExitDate.Value > DateTime.Today))

          .ToListAsync();

       

      When I executed it, I get the following error:

       

      System.NullReferenceException

        HResult=0x80004003

        Message=Object reference not set to an instance of an object.

        Source=Oracle.EntityFrameworkCore

        StackTrace:

         at Oracle.EntityFrameworkCore.Query.Internal.OracleDateTimeMemberTranslator.Translate(SqlExpression instance, MemberInfo member, Type returnType)

         at Microsoft.EntityFrameworkCore.Query.RelationalMemberTranslatorProvider.<>c__DisplayClass3_0.<Translate>b__0(IMemberTranslator t)

         at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()

         at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)

         at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)

         at Microsoft.EntityFrameworkCore.Query.RelationalMemberTranslatorProvider.Translate(SqlExpression instance, MemberInfo member, Type returnType)

         at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMember(MemberExpression memberExpression)

         at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)

         at Oracle.EntityFrameworkCore.Query.Internal.OracleSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)

         at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)

         at Oracle.EntityFrameworkCore.Query.Internal.OracleSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)

         at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)

         at Oracle.EntityFrameworkCore.Query.Internal.OracleSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)

         at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)

         at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression)

         at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression)

         at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression)

         at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)

         at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

         at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

         at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)

         at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)

         at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)

         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)

         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()

         at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)

         at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)

         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)

         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)

         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)

         at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()

         at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__64`1.MoveNext()

         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

         at SFPD.Workflows.Jobs.EvacuationListJob.<ExecuteAsync>d__9.MoveNext() in C:\Development\dotnetcore\SFPD.Workflows\SFPD.Workflows\Jobs\EvacuationList.cs:line 89

       

      However, the following works:

       

      var date = DateTime.Today;

       

      var users = await oracleContext

          .Personnes

          .Where(u => u.Service == "SI" && (u.Floor == "06" || u.Floor == "07") && (u.ExitDate == null || u.ExitDate.Value > date))

          .ToListAsync();

       

      So it seems that using DateTime.Today in a query makes it fail.

      It can also be related to the fact that ExitDate is a DateTime?.

       

      Cheers