Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Date Handling with Dynamic Expressions #13724

Closed
avinash-phaniraj-readify opened this issue Oct 23, 2018 · 3 comments
Closed

Inconsistent Date Handling with Dynamic Expressions #13724

avinash-phaniraj-readify opened this issue Oct 23, 2018 · 3 comments

Comments

@avinash-phaniraj-readify

Using the SQL Provider, passing in a DateTime set to DateTimeKind.Local into a dynamic expression throws exception:

System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'

Exception message: System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'

Stack trace:
System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Conversion failed when converting date and/or time from character string.
  Source=.Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
   at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Microsoft.EntityFrameworkCore.Storage.RelationalDataReader.Read()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TestHost.Program.Main(String[] args) in C:\EFCore\EFCoreTestBed\Program.cs:line 35

Steps to reproduce

Our SQL database uses datetime columns and we have therefore set the Column Attribute's TypeName property to "datetime".

A query such as the one below works as expected. The timezone/offset info appears to be ignored.

private static DateTime _theLocalDate = DateTime.SpecifyKind(new DateTime(2010, 10, 10), DateTimeKind.Local);

var results = context.Set<Employee>()
              .Where(w => w.Created > _theLocalDate)
              .ToList();

When dynamic expressions are employed, this falls over .

      //System.Data.SqlClient.SqlException: 
     //'Conversion failed when converting date and/or time from character string.'
      var results = context.Set<Employee>()
               .Where(GetFilterWithLocalDateTime<Employee>(_theLocalDate))
               .ToList();

        private static Expression<Func<T, bool>> GetFilterWithLocalDateTime<T>(DateTime value)
        {
            var itemParam = Expression.Parameter(typeof(T), "i");

            var filter = Expression.MakeBinary(
                    ExpressionType.GreaterThan,
                    Expression.PropertyOrField(itemParam, "Created"),
                    Expression.Constant(value, typeof(DateTime)));

            return Expression.Lambda<Func<T, bool>>(filter, new[] { itemParam });
        }

Complete repro here --> https://github.com/avinash-phaniraj-readify/EFCoreTestBed/tree/InconsistentDateTimeHandling.

Further technical details

EF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.1.4
Operating system: Win 10
IDE: (e.g. Visual Studio 2017 15.8.6)

@smitpatel
Copy link
Member

Duplicate of #12211

@smitpatel smitpatel marked this as a duplicate of #12211 Oct 23, 2018
@smitpatel
Copy link
Member

@avinash-phaniraj-readify - Can you try using 2.2.0-preview3 from NuGet and see if you still see error?

@avinash-phaniraj-readify
Copy link
Author

avinash-phaniraj-readify commented Oct 23, 2018

@smitpatel
I have tested this on 2.2.0-preview3-35497 and can confirm that it works.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants