Entity Framework 核心 NodaTime 按日期分组
Entity Framework Core NodaTime grouping by date
我正在使用 NodaTime 和 Entity Framework Core,现在我正在尝试按日期对查询进行分组,尽管我使用 Instant
作为字段数据类型。据我所知,使用 dateColumn.Date(当使用 DateTime 时)应该可以工作并将查询传输到 date_trunc('day', dateColumn)
,但是我如何使用 NodaTime Instant
类型实现它?
我看到的问题是 Instant 需要传递时区才能解决日期,这会抛出
Either rewrite the query in a form that can be translated...
如果您需要使用人类日历单位(例如天、月...)执行操作,请考虑改用 LocalDateTime。 Instant的理念恰恰是不支持人工日历操作等
像这样的东西应该可以工作:
_ = await ctx.Blogs.GroupBy(b => b.Creation.Date).CountAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql(@"...", o => o.UseNodaTime());
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public LocalDateTime Creation { get; set; }
}
我正在使用 NodaTime 和 Entity Framework Core,现在我正在尝试按日期对查询进行分组,尽管我使用 Instant
作为字段数据类型。据我所知,使用 dateColumn.Date(当使用 DateTime 时)应该可以工作并将查询传输到 date_trunc('day', dateColumn)
,但是我如何使用 NodaTime Instant
类型实现它?
我看到的问题是 Instant 需要传递时区才能解决日期,这会抛出
Either rewrite the query in a form that can be translated...
如果您需要使用人类日历单位(例如天、月...)执行操作,请考虑改用 LocalDateTime。 Instant的理念恰恰是不支持人工日历操作等
像这样的东西应该可以工作:
_ = await ctx.Blogs.GroupBy(b => b.Creation.Date).CountAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql(@"...", o => o.UseNodaTime());
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public LocalDateTime Creation { get; set; }
}