Skip to content

Commit

Permalink
SqlServer: Generate literal for datetime type correctly
Browse files Browse the repository at this point in the history
Resolves #11262
  • Loading branch information
smitpatel committed Mar 15, 2018
1 parent 2a6f3db commit 9d16a4d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace Microsoft.EntityFrameworkCore.Storage.Internal
/// </summary>
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}";

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand Down Expand Up @@ -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.
/// </summary>
protected override string SqlLiteralFormatString => "'" + DateTimeFormatConst + "'";
protected override string SqlLiteralFormatString
=> StoreType == "date"
? "'" + DateFormatConst + "'"
: (StoreType == "datetime"
? "'" + DateTimeFormatConst + "'"
: "'" + DateTime2FormatConst + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b.Property(c => c.ReportsTo).HasColumnType("int");
});

modelBuilder.Entity<Order>()
.Property(o => o.EmployeeID)
.HasColumnType("int");
modelBuilder.Entity<Order>(
b =>
{
b.Property(o => o.EmployeeID).HasColumnType("int");
b.Property(o => o.OrderDate).HasColumnType("datetime");
});

modelBuilder.Entity<OrderDetail>()
.Property(od => od.UnitPrice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 9d16a4d

Please sign in to comment.