dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

proppery that is datetimeoffset or datetime , function max , min doesnt work on them #34545

Closed mahdiyar021 closed 5 hours ago

mahdiyar021 commented 2 weeks ago

calling max function on string will work but on struct like DateOnly , TimeOnly , DateTime , DateTimeOffset will throw exeption !! i test it on int , long and double and its totally fine .

Context.JwtBlackList.Max(x=>x.Expire) // Expire type is datetimeoffset . throw exeption

Context.JwtBlackList.Max(x=>x.Expire.ToString()) // this will work but call extra function on it

i didnt check it on sqllite or postgres but on sqlSever doent work ! its will be good to be test as well

Include provider and version information

EF Core version: 8 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 8.0) Operating system: IDE: (e.g. Visual Studio 2022 17.4)

roji commented 2 weeks ago

I cannot reproduce the issue; when reporting issues, please always submit a fully runnable console program rather than a small snippet.

You can take a look at the working code below and try to tweak it so it fails.

await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Blogs.Add(new Blog { DateTimeOffset = new DateTimeOffset(2020, 1, 1, 15, 0, 0, TimeSpan.Zero)});
context.Blogs.Add(new Blog { DateTimeOffset = new DateTimeOffset(2021, 1, 1, 15, 0, 0, TimeSpan.Zero)});
await context.SaveChangesAsync();

Console.WriteLine(context.Blogs.Max(b => b.DateTimeOffset));

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, [CoreEventId.QueryCompilationStarting, RelationalEventId.CommandExecuted])
            .EnableSensitiveDataLogging();
}

public class Blog
{
    public int Id { get; set; }

    public DateTimeOffset DateTimeOffset { get; set; }
}
mahdiyar021 commented 2 weeks ago

find the problem. when the query result is null the exception has been throw . 'Sequence contains no elements.' but when you convert it to string it will execute well , Maybe something like max or default would be great . i use (DateTimeOffset?) conevert and its work too but no null allowed . i see that on my complex query too . like select an object inside the object that has property like bool (struct) for some reason database field that is in query return null and it will throw exception . is it my bad design and how can i prevent that ?

mahdiyar021 commented 2 weeks ago

also it will throw exeption when no record is in database !!!

AndriySvyryd commented 5 hours ago

EF Team Triage: This issue is lacking enough information for us to be able to effectively triage it. In particular, it is missing the following information requested in the new issue template. Can you please provide this information?

Steps to reproduce

Ideally include a complete code listing that we can run to reproduce the issue.

Alternatively, you can provide a project/solution that we can run.

BTW we're not just doing this to be mean :smile:... we get a lot traffic on this project and it takes time to attempt to reproduce an issue based on fragments of information. In addition, our attempt is often unsuccessful as the exact conditions required to hit the issue are often not explicitly included in the code provided. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we ask that folks give us a self-contained way to reproduce an issue.

For a guide on submitting good bug reports, read Painless Bug Tracking.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.