Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation of TimeOnly.FromTimeSpan on SQL Server should be no-op. #34025

Open
ajcvickers opened this issue Jun 18, 2024 · 1 comment · May be fixed by #34114
Open

Translation of TimeOnly.FromTimeSpan on SQL Server should be no-op. #34025

ajcvickers opened this issue Jun 18, 2024 · 1 comment · May be fixed by #34114

Comments

@ajcvickers
Copy link
Member

Originally implemented as part of #33678 and found while writing What's New.

Currently, we insert a cast:

context.Set<Animal>().Where(e => timeOnly > TimeOnly.FromTimeSpan(e.TimeSpan))
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
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<Animal>().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<Animal>();
    }
}

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<Owned1> OwnedCollection { get; set; } = null!;
}
@kuznetsovvj
Copy link
Contributor

Hello, I added a check for the type of argument in SqlServerTimeOnlyMethodTranslator.cs #34114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants