Translation of `TimeOnly.FromTimeSpan` on SQL Server should be no-op.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
Originally implemented as part of #33678 and found while writing What's New.
Currently, we insert a cast:
```C#
context.Set().Where(e => timeOnly > TimeOnly.FromTimeSpan(e.TimeSpan))
```
```sql
SELECT [a].[Id], [a].[TimeSpan]
FROM [Animal] AS [a]
WHERE @__timeOnly_0 > CAST([a].[TimeSpan] AS time)
```
But since the `TimeSpan` column is mapped to a `time` column, there doesn't need to be a cast here.
Details
```C#
using (var context = new AppDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Add(new Animal());
await context.SaveChangesAsync();
}
using (var context = new AppDbContext())
{
var timeOnly = TimeOnly.FromDateTime(DateTime.UtcNow);
var animals = await context.Set().Where(e => timeOnly > TimeOnly.FromTimeSpan(e.TimeSpan)).ToListAsync();
}
public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Data Source=localhost;Database=BuildBlogs;Integrated Security=True;Trust Server Certificate=True;ConnectRetryCount=0")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity();
}
}
public class Animal
{
public int Id { get; set; }
public TimeSpan TimeSpan { get; set; }
// public required int[] Ints { get; set; }
// public Owned1 OwnedReference { get; set; } = null!;
// public List OwnedCollection { get; set; } = null!;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.