efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

EF OrderBy DateTime.MinValue fails in .Net 6 Rc2

Open rezabay opened this issue 5 years ago • 3 comments

Source Code

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContextPool<TestContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("Sql"));
});
builder.Services.AddAutoMapper(typeof(ModelMappings));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.MapGet("/projects", async (TestContext db, IMapper mapper) =>
{
    return await db.Projects.OrderBy(x => x.DateCreated ?? DateTime.MinValue).ToListAsync();
});

using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<TestContext>();
if (context.Database.GetPendingMigrations().Any())
{
    context.Database.Migrate();
}

app.Run();

public class ModelMappings : Profile
{
    public ModelMappings()
    {
        CreateMap<ProjectEntity, ProjectModel>();
    }
}

public class ProjectEntity
{
    [Key]
    public int Id { get; set; }
    
    [MaxLength(250)]
    public string Name { get; set; }
    
    public DateTime? DateCreated { get; set; }
}

public class ProjectModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateCreated { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<ProjectEntity> Projects { get; set; }
    
    public TestContext(DbContextOptions options) : base(options)
    {

    }
}

Project file

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-rc.2" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="6.0.0-rc.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-rc.2.21480.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-rc.2.21480.5">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

Exception

InvalidCastException: 'timestamp with time zone' literal cannot be generated for Unspecified DateTime: a UTC DateTime is required Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping.NpgsqlTimestampTzTypeMapping.GenerateLiteralCore(object value) Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping.NpgsqlTimestampTzTypeMapping.GenerateNonNullSqlLiteral(object value)

rezabay avatar Oct 15 '21 04:10 rezabay

Duplicate of #2039

roji avatar Oct 15 '21 07:10 roji

Yeah, this is one of the last remaining things I need to do for the 6.0 release - it should be fixed very soon and you'll be able to use a daily build. In the meantime, you can specify DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc).

roji avatar Oct 15 '21 07:10 roji

@rezabay I've fixed this - can you please try out version 6.0.0-rtm-ci.20211015T155827 from the vNext feed? It would be good to get confirmation that this works well.

... and thanks for trying out the release candidates! This is very valuable and helps catch issues to fix before the final release!

roji avatar Oct 15 '21 16:10 roji