Skip to content

Commit 23e6a9b

Browse files
fixes dotnet#26742
1 parent 6da773f commit 23e6a9b

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/EFCore.SqlServer/Storage/Internal/SqlServerDateTimeTypeMapping.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SqlServerDateTimeTypeMapping : DateTimeTypeMapping
2525
// so the order of the entries in this array is important
2626
private readonly string[] _dateTime2Formats =
2727
{
28-
"'{0:yyyy-MM-ddTHH:mm:ss}'",
28+
"'{0:yyyy-MM-ddTHH:mm:ssK}'",
2929
"'{0:yyyy-MM-ddTHH:mm:ss.fK}'",
3030
"'{0:yyyy-MM-ddTHH:mm:ss.ffK}'",
3131
"'{0:yyyy-MM-ddTHH:mm:ss.fffK}'",
@@ -89,10 +89,21 @@ protected override void ConfigureParameter(DbParameter parameter)
8989

9090
if (Precision.HasValue)
9191
{
92-
parameter.Precision = unchecked((byte)Precision.Value);
92+
// Workaround for inconsistant definition of precision/scale between EF and SQLClient for VarTime types
93+
if (IsVarTime(((SqlParameter)parameter).SqlDbType))
94+
{
95+
parameter.Scale = unchecked((byte)Precision.Value);
96+
}
97+
else
98+
{
99+
parameter.Precision = unchecked((byte)Precision.Value);
100+
}
93101
}
94102
}
95103

104+
private static bool IsVarTime(SqlDbType type) =>
105+
type == SqlDbType.Time || type == SqlDbType.DateTime2 || type == SqlDbType.DateTimeOffset;
106+
96107
/// <summary>
97108
/// Creates a copy of this mapping.
98109
/// </summary>

test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7041,7 +7041,7 @@ public enum BusinessType18346
70417041
public virtual async Task Thread_safety_in_relational_command_cache()
70427042
{
70437043
var contextFactory = await InitializeAsync<MyContext21666>(
7044-
onConfiguring: options =>((IDbContextOptionsBuilderInfrastructure)options).AddOrUpdateExtension(
7044+
onConfiguring: options => ((IDbContextOptionsBuilderInfrastructure)options).AddOrUpdateExtension(
70457045
options.Options.FindExtension<SqlServerOptionsExtension>()
70467046
.WithConnection(null)
70477047
.WithConnectionString(SqlServerTestStore.CreateConnectionString(StoreName))));
@@ -10345,6 +10345,59 @@ public class CollectionViewModel25225
1034510345

1034610346
#endregion
1034710347

10348+
#region Issue26742
10349+
[ConditionalTheory]
10350+
[InlineData(null, "")]
10351+
//[InlineData(0, " (Scale = 0)")] //https://github.com/dotnet/SqlClient/issues/1380 prevents this passing, not EF
10352+
[InlineData(1, " (Scale = 1)")]
10353+
[InlineData(2, " (Scale = 2)")]
10354+
[InlineData(3, " (Scale = 3)")]
10355+
[InlineData(4, " (Scale = 4)")]
10356+
[InlineData(5, " (Scale = 5)")]
10357+
[InlineData(6, " (Scale = 6)")]
10358+
[InlineData(7, " (Scale = 7)")]
10359+
public virtual async Task Query_generates_correct_datetime2_parameter_definition(int? fractionalSeconds, string postfix)
10360+
{
10361+
var contextFactory = await InitializeAsync<MyContext_26742>(onModelCreating: modelBuilder =>
10362+
{
10363+
if (fractionalSeconds.HasValue)
10364+
{
10365+
modelBuilder.Entity<MyContext_26742.Entity>().Property(p => p.DateTime).HasPrecision(fractionalSeconds.Value);
10366+
}
10367+
});
10368+
10369+
var parameter = new DateTime(2021, 11, 12, 13, 14, 15).AddTicks(1234567);
10370+
10371+
using (var context = contextFactory.CreateContext())
10372+
{
10373+
_ = context.Entities.FirstOrDefault(x => x.DateTime == parameter);
10374+
10375+
AssertSql(
10376+
$@"@__parameter_0='2021-11-12T13:14:15.1234567'{postfix}
10377+
10378+
SELECT TOP(1) [e].[Id], [e].[DateTime]
10379+
FROM [Entities] AS [e]
10380+
WHERE [e].[DateTime] = @__parameter_0");
10381+
}
10382+
}
10383+
10384+
protected class MyContext_26742 : DbContext
10385+
{
10386+
public DbSet<Entity> Entities { get; set; }
10387+
10388+
public MyContext_26742(DbContextOptions options)
10389+
: base(options)
10390+
{
10391+
}
10392+
10393+
public class Entity
10394+
{
10395+
public int Id { get; set; }
10396+
public DateTime DateTime { get; set; }
10397+
}
10398+
}
10399+
#endregion
10400+
1034810401
protected override string StoreName => "QueryBugsTest";
1034910402
protected TestSqlLoggerFactory TestSqlLoggerFactory
1035010403
=> (TestSqlLoggerFactory)ListLoggerFactory;

0 commit comments

Comments
 (0)