From 9d16a4de29a49ffbd54866ad2ba283d1cc462878 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Wed, 14 Mar 2018 17:28:14 -0700 Subject: [PATCH] SqlServer: Generate literal for datetime type correctly Resolves #11262 --- .../Storage/Internal/SqlServerDateTimeTypeMapping.cs | 9 ++++++++- .../Query/ComplexNavigationsQuerySqlServerTest.cs | 6 +++--- .../Query/NorthwindQuerySqlServerFixture.cs | 9 ++++++--- .../Query/SimpleQuerySqlServerTest.Functions.cs | 2 +- .../Query/SimpleQuerySqlServerTest.Where.cs | 2 +- .../Query/SimpleQuerySqlServerTest.cs | 2 +- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs b/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs index 782cbc675b7..a92f2c62146 100644 --- a/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs +++ b/src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs @@ -16,7 +16,9 @@ namespace Microsoft.EntityFrameworkCore.Storage.Internal /// public class SqlServerDateTimeTypeMapping : DateTimeTypeMapping { + private const string DateFormatConst = "{0:yyyy-MM-dd}"; private const string DateTimeFormatConst = "{0:yyyy-MM-ddTHH:mm:ss.fffK}"; + private const string DateTime2FormatConst = "{0:yyyy-MM-ddTHH:mm:ss.fffffffK}"; /// /// This API supports the Entity Framework Core infrastructure and is not intended to be used @@ -76,6 +78,11 @@ public override CoreTypeMapping Clone(ValueConverter converter) /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// - protected override string SqlLiteralFormatString => "'" + DateTimeFormatConst + "'"; + protected override string SqlLiteralFormatString + => StoreType == "date" + ? "'" + DateFormatConst + "'" + : (StoreType == "datetime" + ? "'" + DateTimeFormatConst + "'" + : "'" + DateTime2FormatConst + "'"); } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs index 7536ae91ee8..a8dff8fb37a 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/ComplexNavigationsQuerySqlServerTest.cs @@ -374,7 +374,7 @@ public override void Optional_navigation_inside_method_call_translated_to_join_k @"SELECT [e1].[Id], [e1].[Date], [e1].[Name], [e1].[OneToMany_Optional_Self_InverseId], [e1].[OneToMany_Required_Self_InverseId], [e1].[OneToOne_Optional_SelfId] FROM [LevelOne] AS [e1] LEFT JOIN [LevelTwo] AS [e1.OneToOne_Optional_FK] ON [e1].[Id] = [e1.OneToOne_Optional_FK].[Level1_Optional_Id] -WHERE DATEADD(day, 10E0, [e1.OneToOne_Optional_FK].[Date]) > '2000-02-01T00:00:00.000'"); +WHERE DATEADD(day, 10E0, [e1.OneToOne_Optional_FK].[Date]) > '2000-02-01T00:00:00.0000000'"); } public override void Optional_navigation_inside_nested_method_call_translated_to_join_keeps_original_nullability() @@ -385,7 +385,7 @@ public override void Optional_navigation_inside_nested_method_call_translated_to @"SELECT [e1].[Id], [e1].[Date], [e1].[Name], [e1].[OneToMany_Optional_Self_InverseId], [e1].[OneToMany_Required_Self_InverseId], [e1].[OneToOne_Optional_SelfId] FROM [LevelOne] AS [e1] LEFT JOIN [LevelTwo] AS [e1.OneToOne_Optional_FK] ON [e1].[Id] = [e1.OneToOne_Optional_FK].[Level1_Optional_Id] -WHERE DATEADD(month, 2, DATEADD(day, 15E0, DATEADD(day, 10E0, [e1.OneToOne_Optional_FK].[Date]))) > '2002-02-01T00:00:00.000'"); +WHERE DATEADD(month, 2, DATEADD(day, 15E0, DATEADD(day, 10E0, [e1.OneToOne_Optional_FK].[Date]))) > '2002-02-01T00:00:00.0000000'"); } public override void Optional_navigation_inside_nested_method_call_translated_to_join_keeps_original_nullability_also_for_arguments() @@ -396,7 +396,7 @@ public override void Optional_navigation_inside_nested_method_call_translated_to @"SELECT [e1].[Id], [e1].[Date], [e1].[Name], [e1].[OneToMany_Optional_Self_InverseId], [e1].[OneToMany_Required_Self_InverseId], [e1].[OneToOne_Optional_SelfId] FROM [LevelOne] AS [e1] LEFT JOIN [LevelTwo] AS [e1.OneToOne_Optional_FK] ON [e1].[Id] = [e1.OneToOne_Optional_FK].[Level1_Optional_Id] -WHERE DATEADD(day, [e1.OneToOne_Optional_FK].[Id], DATEADD(day, 15E0, [e1.OneToOne_Optional_FK].[Date])) > '2002-02-01T00:00:00.000'"); +WHERE DATEADD(day, [e1.OneToOne_Optional_FK].[Id], DATEADD(day, 15E0, [e1.OneToOne_Optional_FK].[Date])) > '2002-02-01T00:00:00.0000000'"); } public override void Join_navigation_in_outer_selector_translated_to_extra_join() diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQuerySqlServerFixture.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQuerySqlServerFixture.cs index bcf140534ca..955253f569f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQuerySqlServerFixture.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindQuerySqlServerFixture.cs @@ -27,9 +27,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con b.Property(c => c.ReportsTo).HasColumnType("int"); }); - modelBuilder.Entity() - .Property(o => o.EmployeeID) - .HasColumnType("int"); + modelBuilder.Entity( + b => + { + b.Property(o => o.EmployeeID).HasColumnType("int"); + b.Property(o => o.OrderDate).HasColumnType("datetime"); + }); modelBuilder.Entity() .Property(od => od.UnitPrice) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Functions.cs b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Functions.cs index d5eaa17e66f..37a63922b44 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Functions.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Functions.cs @@ -1286,7 +1286,7 @@ public override void Static_equals_nullable_datetime_compared_to_non_nullable() base.Static_equals_nullable_datetime_compared_to_non_nullable(); AssertSql( - @"@__arg_0='1996-07-04T00:00:00' + @"@__arg_0='1996-07-04T00:00:00' (DbType = DateTime) SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate] FROM [Orders] AS [o] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs index 0d7443a3506..83526ea4597 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.Where.cs @@ -750,7 +750,7 @@ public override void Where_datetime_date_component() base.Where_datetime_date_component(); AssertSql( - @"@__myDatetime_0='1998-05-04T00:00:00' + @"@__myDatetime_0='1998-05-04T00:00:00' (DbType = DateTime) SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate] FROM [Orders] AS [o] diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.cs index e887016eb67..e57f04960d8 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/SimpleQuerySqlServerTest.cs @@ -2568,7 +2568,7 @@ public override void DateTime_parse_is_parameterized() base.DateTime_parse_is_parameterized(); AssertSql( - @"@__Parse_0='1998-01-01T12:00:00' + @"@__Parse_0='1998-01-01T12:00:00' (DbType = DateTime) SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate] FROM [Orders] AS [o]