Skip to content

Commit

Permalink
fixes dotnet#26742
Browse files Browse the repository at this point in the history
  • Loading branch information
EamonHetherton committed Nov 18, 2021
1 parent 6da773f commit 23e6a9b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SqlServerDateTimeTypeMapping : DateTimeTypeMapping
// so the order of the entries in this array is important
private readonly string[] _dateTime2Formats =
{
"'{0:yyyy-MM-ddTHH:mm:ss}'",
"'{0:yyyy-MM-ddTHH:mm:ssK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.fK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.ffK}'",
"'{0:yyyy-MM-ddTHH:mm:ss.fffK}'",
Expand Down Expand Up @@ -89,10 +89,21 @@ protected override void ConfigureParameter(DbParameter parameter)

if (Precision.HasValue)
{
parameter.Precision = unchecked((byte)Precision.Value);
// Workaround for inconsistant definition of precision/scale between EF and SQLClient for VarTime types
if (IsVarTime(((SqlParameter)parameter).SqlDbType))
{
parameter.Scale = unchecked((byte)Precision.Value);
}
else
{
parameter.Precision = unchecked((byte)Precision.Value);
}
}
}

private static bool IsVarTime(SqlDbType type) =>
type == SqlDbType.Time || type == SqlDbType.DateTime2 || type == SqlDbType.DateTimeOffset;

/// <summary>
/// Creates a copy of this mapping.
/// </summary>
Expand Down
55 changes: 54 additions & 1 deletion test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7041,7 +7041,7 @@ public enum BusinessType18346
public virtual async Task Thread_safety_in_relational_command_cache()
{
var contextFactory = await InitializeAsync<MyContext21666>(
onConfiguring: options =>((IDbContextOptionsBuilderInfrastructure)options).AddOrUpdateExtension(
onConfiguring: options => ((IDbContextOptionsBuilderInfrastructure)options).AddOrUpdateExtension(
options.Options.FindExtension<SqlServerOptionsExtension>()
.WithConnection(null)
.WithConnectionString(SqlServerTestStore.CreateConnectionString(StoreName))));
Expand Down Expand Up @@ -10345,6 +10345,59 @@ public class CollectionViewModel25225

#endregion

#region Issue26742
[ConditionalTheory]
[InlineData(null, "")]
//[InlineData(0, " (Scale = 0)")] //https://github.com/dotnet/SqlClient/issues/1380 prevents this passing, not EF
[InlineData(1, " (Scale = 1)")]
[InlineData(2, " (Scale = 2)")]
[InlineData(3, " (Scale = 3)")]
[InlineData(4, " (Scale = 4)")]
[InlineData(5, " (Scale = 5)")]
[InlineData(6, " (Scale = 6)")]
[InlineData(7, " (Scale = 7)")]
public virtual async Task Query_generates_correct_datetime2_parameter_definition(int? fractionalSeconds, string postfix)
{
var contextFactory = await InitializeAsync<MyContext_26742>(onModelCreating: modelBuilder =>
{
if (fractionalSeconds.HasValue)
{
modelBuilder.Entity<MyContext_26742.Entity>().Property(p => p.DateTime).HasPrecision(fractionalSeconds.Value);
}
});

var parameter = new DateTime(2021, 11, 12, 13, 14, 15).AddTicks(1234567);

using (var context = contextFactory.CreateContext())
{
_ = context.Entities.FirstOrDefault(x => x.DateTime == parameter);

AssertSql(
$@"@__parameter_0='2021-11-12T13:14:15.1234567'{postfix}
SELECT TOP(1) [e].[Id], [e].[DateTime]
FROM [Entities] AS [e]
WHERE [e].[DateTime] = @__parameter_0");
}
}

protected class MyContext_26742 : DbContext
{
public DbSet<Entity> Entities { get; set; }

public MyContext_26742(DbContextOptions options)
: base(options)
{
}

public class Entity
{
public int Id { get; set; }
public DateTime DateTime { get; set; }
}
}
#endregion

protected override string StoreName => "QueryBugsTest";
protected TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
Expand Down

0 comments on commit 23e6a9b

Please sign in to comment.